Skip to content

Commit 3075e48

Browse files
committed
improve rich text edition
#913 (reply in thread)
1 parent 84d9594 commit 3075e48

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

examples/rich-text-editor/edit.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
insert or replace into blog_posts (id, title, content)
2+
select $id, :title, :content
3+
where $id is not null and :title is not null and :content is not null
4+
returning 'redirect' as component, 'post?id=' || $id as link;
5+
6+
select 'shell' as component,
7+
'Edit blog post' as title,
8+
'/rich_text_editor.js' as javascript_module;
9+
10+
11+
select 'form' as component, 'Update' as validate;
12+
13+
with post as (
14+
select title, content
15+
from blog_posts
16+
where id = $id
17+
),
18+
fields as (
19+
select json_object(
20+
'name', 'title',
21+
'label', 'Blog post title',
22+
'value', title
23+
) as props
24+
from post
25+
union all
26+
select json_object(
27+
'name', 'content',
28+
'type', 'textarea',
29+
'label', 'Your blog post here',
30+
'value', content,
31+
'required', true
32+
)
33+
from post
34+
)
35+
select 'dynamic' as component, json_group_array(props) as properties from fields;

examples/rich-text-editor/post.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ select 'text' as component,
88
content as contents_md
99
from blog_posts
1010
where id = $id;
11+
12+
select 'list' as component;
13+
select
14+
'Edit' as title,
15+
'pencil' as icon,
16+
'edit?id=' || $id as link;

examples/rich-text-editor/rich_text_editor.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ function traverseMdastNode(node, delta, attributes = {}) {
214214

215215
case "listItem":
216216
for (const child of node.children || []) {
217-
traverseMdastNode(child, delta, attributes);
217+
traverseMdastNode(child, delta, {});
218+
}
219+
{
220+
const lastOp = delta.ops[delta.ops.length - 1];
221+
if (lastOp && lastOp.insert === "\n") lastOp.attributes = attributes;
222+
else delta.ops.push({ insert: "\n", attributes });
218223
}
219224
break;
220225

@@ -269,6 +274,7 @@ function updateTextareaOnSubmit(form, textarea, quill) {
269274
const delta = quill.getContents();
270275
const markdownContent = deltaToMarkdown(delta);
271276
textarea.value = markdownContent;
277+
console.log(`${textarea.name}:\n${markdownContent}\ntransformed from delta:\n${JSON.stringify(delta, null, 2)}`);
272278
if (textarea.required && !markdownContent) {
273279
textarea.setCustomValidity(`${textarea.name} cannot be empty`);
274280
quill.once("text-change", (delta) => {
@@ -421,8 +427,9 @@ function deltaToMdast(delta) {
421427
textBuffer = line;
422428
}
423429

424-
// Process line break with empty attributes (regular paragraph break)
425-
processLineBreak(mdast, currentParagraph, {}, textBuffer, currentList);
430+
// Process line break
431+
currentList = processLineBreak(mdast, currentParagraph, attributes, textBuffer, currentList);
432+
426433
currentParagraph = null;
427434
textBuffer = "";
428435
}

0 commit comments

Comments
 (0)