Skip to content

Commit e9cc4e7

Browse files
committed
Use two wrappers to allow interactive elements in live preview
1 parent 09f361b commit e9cc4e7

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,29 @@ And for more complex cases there is support for JSX. JSX is automatically wrappe
9696
```
9797
````
9898

99+
If your component is interactive (e.g. react to user clicks), in Live Preview mode clicking on such component will switch it in editing mode. If this behavior isn't desired, wrap your component in another element and stop propagation of click events.
100+
101+
```jsx
102+
<div onClick={e => e.stopPropagation()}>
103+
{/* Your component markup goes here... */}
104+
</div>
105+
```
106+
99107
### Vanilla JavaScript
100108

101109
In addition to components, you can use vanilla JavaScript. JS code can be used either inline or as block. For JS code use language specifier `emjs` instead of `emera`.
102110

103111
Inline JS will be evaluated and its result will replace original code element on page. For example, this snippet will output current vault name ```emjs: app.vault.getName()```.
104112

105-
JS code blocks are more powerful. They don't output anything directly, but you can use them for more complex operations and to add variables to page's scope (more about scope a bit later).
113+
JS code blocks are more powerful. They don't output anything directly, but you can use them for more complex operations and to add variables to page's scope (more about scopes a bit later).
106114

107115
````markdown
108116
```emjs
109117
export const username = 'OlegWock';
110118
```
111119
````
112120

113-
Variable `username` will be available to all JS and JSX code on this page (after original `emjs` code block).
121+
Variable `username` will be available to all JS and JSX code on this page (after the original `emjs` code block).
114122

115123
### Scope
116124

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "emera",
33
"name": "Emera",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"minAppVersion": "1.6.5",
66
"description": "Enables you to use custom React components and inline JavaScript, kinda like MDX.",
77
"author": "OlegWock",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-emera",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "MDX for Obsidian. Kinda.",
55
"main": "main.js",
66
"scripts": {

src/processors/code-processor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,16 @@ export class EmeraCodeProcessor {
266266

267267
toDOM(view: EditorView): HTMLElement {
268268
const wrapper = document.createElement(inline ? 'span' : 'div');
269+
const reactRootWrapper = document.createElement(inline ? 'span' : 'div');
270+
wrapper.appendChild(reactRootWrapper);
269271
wrapper.addEventListener('click', (e) => {
270-
e.stopImmediatePropagation();
271-
});
272-
wrapper.addEventListener('dblclick', (e) => {
273272
e.preventDefault();
274273
view.dispatch({
275274
selection: { anchor: view.posAtDOM(wrapper) },
276275
scrollIntoView: true
277276
});
278277
});
279-
func(wrapper, this.content, this.ctx);
278+
func(reactRootWrapper, this.content, this.ctx);
280279
return wrapper;
281280
}
282281
}

0 commit comments

Comments
 (0)