|
1 | 1 | --- |
2 | | -text: asdakdbg |
| 2 | +text: this is some text |
3 | 3 | number: 12234234 |
4 | 4 | --- |
5 | 5 |
|
@@ -49,77 +49,91 @@ return markdownBuilder |
49 | 49 |
|
50 | 50 | # Import Test |
51 | 51 |
|
| 52 | +You can import JS files that lie inside of your vault. |
| 53 | + |
| 54 | +JS Engine version: |
| 55 | + |
52 | 56 | ```js-engine |
53 | 57 | let lib = await engine.importJs("lib.js"); |
54 | 58 | return lib.getGreeting(); |
55 | 59 | ``` |
56 | 60 |
|
| 61 | +Modules Plugin version: |
| 62 | + |
57 | 63 | ```js-engine |
58 | 64 | let lib = await self.require.import("lib.js"); |
59 | 65 | return lib.getGreeting(); |
60 | 66 | ``` |
61 | 67 |
|
| 68 | +# Lib Test |
| 69 | + |
| 70 | +Importing packaged libraries works. In this example [parsiNOM](https://github.com/mProjectsCode/parsiNOM) is used. |
| 71 | + |
62 | 72 | ```js-engine |
63 | | -let lib = await engine.importJs("svelte_project/dist/assets/index-9a060cab.js"); |
64 | | -console.log(lib); |
65 | | -console.log(lib.App); |
| 73 | +let { P, P_UTILS } = engine.lib.parsinom(); |
| 74 | +
|
| 75 | +const parser = P.sequence( |
| 76 | + P_UTILS.letters(), |
| 77 | + P_UTILS.digits() |
| 78 | +).map( |
| 79 | + ([a, b]) => b + a |
| 80 | +); |
| 81 | +
|
| 82 | +return parser.parse("test123"); |
66 | 83 | ``` |
67 | 84 |
|
68 | | -# Async Test |
| 85 | +This example uses [IterTools](https://github.com/Smoren/itertools-ts). |
69 | 86 |
|
70 | 87 | ```js-engine |
71 | | -return await engine.test() |
| 88 | +let { Stream } = engine.lib.itertools(); |
| 89 | +
|
| 90 | +const result = Stream.of([1, 1, 2, 2, 3, 4, 5]) |
| 91 | + .distinct() // [1, 2, 3, 4, 5] |
| 92 | + .map((x) => x ** 2) // [1, 4, 9, 16, 25] |
| 93 | + .filter((x) => x < 10) // [1, 4, 9] |
| 94 | + .toSum(); // 14 |
| 95 | +
|
| 96 | +return result; |
72 | 97 | ``` |
73 | 98 |
|
74 | 99 | # Meta Bind Test |
75 | 100 |
|
| 101 | +You can interact with other plugins APIs, in this example [Meta Bind](https://github.com/mProjectsCode/obsidian-meta-bind-plugin) is used. |
| 102 | + |
76 | 103 | ```js-engine |
77 | | -const meta_bind_api = engine.getPlugin("obsidian-meta-bind-plugin").api |
| 104 | +const mb = engine.getPlugin("obsidian-meta-bind-plugin").api |
78 | 105 |
|
79 | 106 | const div1 = container.createDiv() |
80 | 107 | const div2 = container.createDiv() |
81 | 108 |
|
82 | | -const inputField = meta_bind_api.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, div1); |
83 | | -const inputField2 = meta_bind_api.createInputFieldFromString("INPUT[number:number]", "INLINE", context.file.path, div2); |
84 | | -
|
85 | | -component.addChild(inputField) |
86 | | -component.addChild(inputField2) |
| 109 | +const inputField = mb.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, div1, component, undefined); |
| 110 | +const inputField2 = mb.createInputFieldFromString("INPUT[number:number]", "INLINE", context.file.path, div2, component, undefined); |
87 | 111 | ``` |
88 | 112 |
|
89 | | -# Reactive Idea (TODO) |
90 | | - |
91 | | -```js |
92 | | -// define a function that takes in some args and returns what should be rendered |
93 | | -function render(args) { |
94 | | - return 'Something'; |
95 | | -} |
96 | | - |
97 | | -// create a reactive component, passing in the function and arguments for the initial render |
98 | | -const reactive = engine.reactive(render, initialArgs); |
| 113 | +# Reactive Components |
99 | 114 |
|
100 | | -// subscribe to events and call refresh with new arguments, which causes the render function to be rerun with these arguments, replacing the existing content |
101 | | -event.on('...', (args) => reactive.refresh(args)); |
102 | | - |
103 | | -// return the reactive component |
104 | | -return reactive; |
105 | | -``` |
| 115 | +You can also create reactive components that re-render based on a specific event. In this case we subscribe to obsidians metadata cache. |
106 | 116 |
|
107 | 117 | ```js-engine |
108 | 118 |
|
| 119 | +// define a function that takes in some args and returns what should be rendered |
109 | 120 | function render(args) { |
110 | | - console.log(args) |
111 | 121 | return args?.text ?? "undefined"; |
112 | 122 | } |
113 | 123 |
|
| 124 | +// create a reactive component, passing in the function and arguments for the initial render |
114 | 125 | const reactive = engine.reactive(render, context.metadata.frontmatter); |
115 | 126 |
|
| 127 | +// subscribe to events and call refresh with new arguments, which causes the render function to be rerun with these arguments, replacing the existing content |
116 | 128 | const unloadCb = engine.app.metadataCache.on('changed', async (file, data, cache) => { |
117 | 129 | if (file.path === context.file.path) { |
118 | 130 | reactive.refresh(cache.frontmatter); |
119 | 131 | } |
120 | 132 | }); |
121 | 133 |
|
| 134 | +// register the subscription to be unloaded when the code block is unloaded |
122 | 135 | component.registerEvent(unloadCb); |
123 | 136 |
|
| 137 | +// return the reactive component |
124 | 138 | return reactive; |
125 | 139 | ``` |
0 commit comments