|
| 1 | +--- |
| 2 | +select: b |
| 3 | +--- |
| 4 | + |
| 5 | +## Creating an Input Field with JS Engine |
| 6 | + |
| 7 | +JS Engine can be found [here](https://github.com/mProjectsCode/obsidian-js-engine-plugin). |
| 8 | + |
| 9 | +**Code** |
| 10 | +```js |
| 11 | +const mb = engine.getPlugin('obsidian-meta-bind-plugin').api; |
| 12 | + |
| 13 | +const options = ['a', 'b', 'c']; |
| 14 | + |
| 15 | +// first, create an empty declaration |
| 16 | +let declaration = mb.inputField.createInputFieldDeclaration(); |
| 17 | +// set the input field type |
| 18 | +declaration = mb.inputField.setType(declaration, 'select'); |
| 19 | +// bind the input field to 'select' |
| 20 | +declaration = mb.inputField.setBindTargetMetadataField(declaration, 'select'); |
| 21 | + |
| 22 | +for (const option of options) { |
| 23 | + // add all the options |
| 24 | + declaration = mb.inputField.addArgument(declaration, {name: 'option', value: [option]}); |
| 25 | +} |
| 26 | + |
| 27 | +// create the input field in the container and pass in the component for life cycle management (container and component are globals exposed by js engine) |
| 28 | +mb.createInputField(declaration, 'block', context.file.path, container, component); |
| 29 | +``` |
| 30 | + |
| 31 | +**Resulting Input Field** |
| 32 | +```js-engine |
| 33 | +const mb = engine.getPlugin('obsidian-meta-bind-plugin').api; |
| 34 | +
|
| 35 | +const options = ['a', 'b', 'c']; |
| 36 | +
|
| 37 | +// first, create an empty declaration |
| 38 | +let declaration = mb.inputField.createInputFieldDeclaration(); |
| 39 | +// set the input field type |
| 40 | +declaration = mb.inputField.setType(declaration, 'select'); |
| 41 | +// bind the input field to 'select' |
| 42 | +declaration = mb.inputField.setBindTargetMetadataField(declaration, 'select'); |
| 43 | +
|
| 44 | +for (const option of options) { |
| 45 | + // add all the options |
| 46 | + declaration = mb.inputField.addArgument(declaration, {name: 'option', value: [option]}); |
| 47 | +} |
| 48 | +
|
| 49 | +declaration = mb.inputField.addArgument(declaration, {name: 'title', value: ['I was created using JS Engine and the Meta Bind API']}); |
| 50 | +
|
| 51 | +// create the input field in the container and pass in the component for life cycle management (container and component are globals exposed by js engine) |
| 52 | +mb.createInputField(declaration, 'block', context.file.path, container, component); |
| 53 | +``` |
0 commit comments