Skip to content

Commit 8b7e1c9

Browse files
committed
Demo: insert blank section after inserting card
1 parent cdd1260 commit 8b7e1c9

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

demo/demo.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Editor, UI } from './mobiledoc.js'
33
const card = {
44
name: 'image',
55
type: 'dom',
6-
render({ env, opts, payload }) {
6+
render({ env, payload }) {
77
const el = document.createElement('div')
88
const img = document.createElement('img')
99
const caption = document.createElement('figcaption')
@@ -17,7 +17,7 @@ const card = {
1717
el.appendChild(button)
1818
return el
1919
},
20-
edit({ env, opts, payload }) {
20+
edit({ env, payload }) {
2121
const el = document.createElement('div')
2222
const img = document.createElement('img')
2323
const button = document.createElement('button')
@@ -64,14 +64,20 @@ function activateButtons(parentSelector, editor) {
6464
button.addEventListener('click', () => {
6565
const action = button.getAttribute('data-action')
6666
const args = button.getAttribute('data-args').split(',')
67-
const payload = (function () {
68-
try {
69-
return JSON.parse(button.getAttribute('data-payload'))
70-
} catch {}
71-
})()
72-
if (args[0] === 'a') UI[action](editor)
73-
else if (payload) editor[action](...args, payload)
74-
else editor[action](...args)
67+
if (args[0] === 'a') {
68+
UI[action](editor)
69+
} else if (action === 'insertCard') {
70+
args[1] = JSON.parse(args[1])
71+
editor[action](...args)
72+
// Insert a blank paragraph section after card to continue typing
73+
editor.run(postEditor => {
74+
const section = postEditor.builder.createMarkupSection()
75+
postEditor.insertSectionBefore(editor.post.sections, section, editor.activeSection.next)
76+
postEditor.setRange(section.toRange())
77+
})
78+
} else {
79+
editor[action](...args)
80+
}
7581
})
7682
)
7783
}
@@ -98,7 +104,7 @@ function bootstrapEditor() {
98104
const activeSectionTags = editor.activeSections.map(s => (s.isNested ? s.parent.tagName : s.tagName))
99105
const activeAttributeVals = editor.activeSections.map(s => Object.values(s.attributes || {}))
100106
const allActive = [...activeMarkupTags, ...activeSectionTags, ...activeAttributeVals]
101-
const selector = allActive.map(arg => `[data-args*="${arg}"]`).join(',')
107+
const selector = allActive.map(arg => `[data-args="${arg}"],[data-args="text-align,${arg}"]`).join(',')
102108
document.querySelectorAll(`[data-args]`).forEach(el => el.classList.remove('active'))
103109
selector && document.querySelectorAll(selector).forEach(el => el.classList.add('active'))
104110
})

demo/index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@
175175
</button>
176176
</div>
177177
<div class="toolbar-section">
178-
<button data-action="insertCard" data-args="image" data-payload='{"caption":"Image of a kitten"}'>
179-
Image Card
180-
</button>
181-
<button data-action="insertAtom" data-args="clicker" data-payload="">Clicker Atom</button>
178+
<button data-action="insertCard" data-args='image,{"caption":"Image of a kitten"}'>Image Card</button>
179+
<button data-action="insertAtom" data-args="clicker,''">Clicker Atom</button>
182180
</div>
183181
</div>
184182
<div id="editor"></div>
@@ -384,10 +382,10 @@ <h2>Cards and Atoms</h2>
384382
<div class="column right">
385383
<div class="editor-wrapper" id="editor-card-wrapper">
386384
<div class="toolbar">
387-
<button data-action="insertCard" data-args="image" data-payload='{"caption":"Image of a kitten"}'>
385+
<button data-action="insertCard" data-args='image,{"caption":"Image of a kitten"}'>
388386
Insert Kitten Card
389387
</button>
390-
<button data-action="insertAtom" data-args="clicker" data-payload="">Insert Click-Counter Atom</button>
388+
<button data-action="insertAtom" data-args="clicker,''">Insert Click-Counter Atom</button>
391389
</div>
392390
<div id="editor-card" class="editor"></div>
393391
</div>

0 commit comments

Comments
 (0)