Ability to render MetaBind fields in a Note Toolbar modal #508
Replies: 4 comments 3 replies
-
To be precise, my suggestion was NOT about passing to the constructor, but creating it within the modal itself. Passing external component raises questions about who manages its lifecycle... |
Beta Was this translation helpful? Give feedback.
-
|
@azaol-aegnor Can you please provide more detail about what you tried to pass into |
Beta Was this translation helpful? Give feedback.
-
|
Ahoy! Thanks for your motivation to investigate it in your plugin! Where is the Component missingTo second what @mnaoumov said just up there, the question is about who handles the life cycle of the component. Initially, I was expecting Obsidian's Modal to inherit from Component because it implements CloseableComponent. Meaning it was a Component itself, and it would be destroyed/cleared on closing. To come back to the problem, MetaBind is requiring a component in the first link you provided. Because the Modal itself does not inherits from Component, I can't just forward it directly (as I feel we should, but that's a "problem" from Obsidian's API, not NotetoolBar). So the second best solution would be for the Modal itself to hold a Component instance that scripts who create a Modal could reference to, deleguating its lifecycle management to the Modal (that would call unload() when the Modal is closed/destroyed). Introduction to the problemHere's how I was trying to use it: // MARK: Imports (sort of)
/**
* NoteToolbar's JS script context
* @external INoteToolbarApi
* @see {@link https://github.com/chrisgurney/obsidian-note-toolbar|obsidian-note-toolbar}
*/
// @ts-ignore TS2304
const context = ntb
/**
* Obsidian API
* @external Obsidian
* @see {@link https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts|obsidian.d.ts}
*/
const obsidian = context.o
/**
* Obsidian's App instance (not from global scope)
* @external Obsidian.App
* @see {@link https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts#L406|obsidian.d.ts#App}
*/
const lapp = context.app
// MARK: Script
const currFile = lapp.workspace.getActiveFileView()?.file
const textContent = `Hi!
Simple demo of a field to edit the dueDate property:
~~~meta-bind
INPUT[datePicker:${currFile.path}#dueDate]
~~~
This text is displayed, but the field is not.
`
const modal = await ntb.modal(textContent , {
title: `**${currFile.basename} quick properties form**`
})For some reason, the field does not show up. If you copy the textContent variable to a file, replace The problem itselfNow we're getting to the point. Because the Modal coudn't show the field directly (and/or if I wanted to do advanced things), I tried using MetaBind's API to build the field. Here's what the same block as above looks like (spiking my first three fake imports): // MARK: Script
const currFile = lapp.workspace.getActiveFileView()?.file
const textContent = `Hi!\n\n`
const modal = await ntb.modal(textContent , {
title: `**${currFile.basename} quick properties form**`
})
const mb = lapp.plugins.getPlugin('obsidian-meta-bind-plugin').api
const opts = {
declaration: {
inputFieldType: 'number',
},
renderChildType: 'block',
}
const inputField = mb.createInputFieldMountable(currFile.path, opts)
mb.wrapInMDRC(inputField, modal.contentEl, modal)The last line throws an exception, basically because modal is not a Component. With our suggestion of the NtbModal holding a component for us, I'd expect the last line to become one of: mb.wrapInMDRC(inputField, modal.contentEl, modal.component)
mb.wrapInMDRC(inputField, modal.contentEl, modal.getReferenceToComponent())Feel free to ping me on Discord (and/or DM), plan a quick call, ask for demo files to be attached or ask any follow-up questions! |
Beta Was this translation helpful? Give feedback.
-
|
Here are a few files to demo the usage a little.
I added the two working embeds in I basically try to embed the third file in a modal when clicking a NTB button in the first ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Per @azaol-aegnor on Discord:
MetaBind asks for a component in the signature, it's to ensure the field created is linked to a parent entity for life cycle management: https://www.moritzjung.dev/obsidian-meta-bind-plugin-docs/guides/api/#input-fields
On Discord, mnaoumov suggests an implementation for modals in which a component is
passedcreated in the constructor:https://discord.com/channels/686053708261228577/840286264964022302/1470876274426773525
Here is Note Toolbar's
ntb.modal()API implementation:https://github.com/chrisgurney/obsidian-note-toolbar/blob/master/src/Api/NtbModal.ts
Beta Was this translation helpful? Give feedback.
All reactions