@functionalcms/svelte-components 5.10.0 → 5.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,102 +1,109 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
turnIntoTextCommand,
|
|
17
|
-
} from "@milkdown/kit/preset/commonmark";
|
|
18
|
-
import { getHTML } from "@milkdown/kit/utils";
|
|
19
|
-
import { listener, listenerCtx } from "@milkdown/kit/plugin/listener";
|
|
20
|
-
import { Button, ButtonGroup } from "@functionalcms/svelte-components";
|
|
2
|
+
import { Editor, rootCtx, commandsCtx, defaultValueCtx } from '@milkdown/kit/core';
|
|
3
|
+
import { commonmark } from '@milkdown/kit/preset/commonmark';
|
|
4
|
+
import { history } from '@milkdown/kit/plugin/history';
|
|
5
|
+
import { nord } from '@milkdown/theme-nord';
|
|
6
|
+
import {
|
|
7
|
+
wrapInHeadingCommand,
|
|
8
|
+
toggleStrongCommand,
|
|
9
|
+
toggleEmphasisCommand,
|
|
10
|
+
wrapInBulletListCommand,
|
|
11
|
+
turnIntoTextCommand
|
|
12
|
+
} from '@milkdown/kit/preset/commonmark';
|
|
13
|
+
import { getHTML } from '@milkdown/kit/utils';
|
|
14
|
+
import { listener, listenerCtx } from '@milkdown/kit/plugin/listener';
|
|
15
|
+
import { Button, ButtonGroup } from '@functionalcms/svelte-components';
|
|
21
16
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
interface Props {
|
|
18
|
+
id: string;
|
|
19
|
+
initialValue: string;
|
|
20
|
+
markdown: string;
|
|
21
|
+
html: string;
|
|
22
|
+
type: 'markdown' | 'html';
|
|
23
|
+
}
|
|
27
24
|
|
|
28
|
-
|
|
25
|
+
let {
|
|
26
|
+
id = '',
|
|
27
|
+
initialValue = '',
|
|
28
|
+
markdown = $bindable(initialValue),
|
|
29
|
+
html = $bindable(initialValue),
|
|
30
|
+
type = 'markdown'
|
|
31
|
+
}: Partial<Props> = $props();
|
|
29
32
|
|
|
30
|
-
|
|
33
|
+
let editorInstance: Editor | null = null;
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
const applyCommand = (command: any, params?: any) => {
|
|
36
|
+
if (!editorInstance) return;
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
try {
|
|
39
|
+
editorInstance.action((ctx) => {
|
|
40
|
+
const commandManager = ctx.get(commandsCtx);
|
|
41
|
+
commandManager.call(command, params);
|
|
42
|
+
});
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error('Command error:', e);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const setHeadingCommand = (level: 1 | 2 | 3 | 4 | 5 | 6) =>
|
|
49
|
+
applyCommand(wrapInHeadingCommand.key, level);
|
|
50
|
+
const makeBold = () => applyCommand(toggleStrongCommand.key);
|
|
51
|
+
const makeItalic = () => applyCommand(toggleEmphasisCommand.key);
|
|
52
|
+
const makeList = () => applyCommand(wrapInBulletListCommand.key);
|
|
53
|
+
const clearFormatting = () => applyCommand(turnIntoTextCommand.key);
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
55
|
+
let value = $state(initialValue);
|
|
56
|
+
|
|
57
|
+
function editor(dom) {
|
|
58
|
+
const MakeEditor = Editor.make()
|
|
59
|
+
.config((ctx) => {
|
|
60
|
+
ctx.set(rootCtx, dom);
|
|
61
|
+
ctx.set(defaultValueCtx, initialValue);
|
|
62
|
+
ctx.get(listenerCtx).markdownUpdated((ctx, markdownContent) => {
|
|
63
|
+
markdown = markdownContent;
|
|
64
|
+
html = getHTML()(ctx);
|
|
65
|
+
value = type === 'markdown' ? markdownContent : html;
|
|
66
|
+
});
|
|
67
|
+
})
|
|
68
|
+
.config(nord)
|
|
69
|
+
.use(commonmark)
|
|
70
|
+
.use(listener)
|
|
71
|
+
.use(history)
|
|
72
|
+
.create();
|
|
73
|
+
MakeEditor.then((editor) => {
|
|
74
|
+
editorInstance = editor;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
71
77
|
</script>
|
|
72
78
|
|
|
73
79
|
<div class="editor-container">
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
<ButtonGroup>
|
|
81
|
+
<Button onclick={() => setHeadingCommand(1)}>H1</Button>
|
|
82
|
+
<Button onclick={() => setHeadingCommand(2)}>H2</Button>
|
|
83
|
+
<Button onclick={() => setHeadingCommand(3)}>H3</Button>
|
|
84
|
+
<Button onclick={() => setHeadingCommand(4)}>H4</Button>
|
|
85
|
+
<Button onclick={() => setHeadingCommand(5)}>H5</Button>
|
|
86
|
+
<Button onclick={() => setHeadingCommand(6)}>H6</Button>
|
|
87
|
+
<Button onclick={() => makeBold()}><strong>B</strong></Button>
|
|
88
|
+
<Button onclick={() => makeItalic()}><em>I</em></Button>
|
|
89
|
+
<Button onclick={() => makeList()}>List</Button>
|
|
90
|
+
<Button onclick={() => clearFormatting()}>Clear</Button>
|
|
91
|
+
</ButtonGroup>
|
|
92
|
+
<div class="editor-body" use:editor></div>
|
|
93
|
+
<input type="hidden" name={id} bind:value />
|
|
87
94
|
</div>
|
|
88
95
|
|
|
89
96
|
<style>
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
+
.editor-container {
|
|
98
|
+
display: flex;
|
|
99
|
+
flex-direction: column;
|
|
100
|
+
gap: 0.5rem;
|
|
101
|
+
border: 2px solid black;
|
|
102
|
+
border-radius: 5px;
|
|
103
|
+
}
|
|
97
104
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
.editor-body {
|
|
106
|
+
background: white;
|
|
107
|
+
height: 100%;
|
|
108
|
+
}
|
|
102
109
|
</style>
|
|
@@ -238,14 +238,12 @@
|
|
|
238
238
|
<Label for={field.id} class={field.class}>{field.label}</Label>
|
|
239
239
|
{#key field.id}
|
|
240
240
|
<Editor initialValue={field.value} bind:markdown={field.value} />
|
|
241
|
-
<input type="hidden" name={field.id} bind:value={field.value} />
|
|
242
241
|
{/key}
|
|
243
242
|
{/snippet}
|
|
244
243
|
{#snippet renderHtmlEditory(field: HtmlEditorField)}
|
|
245
244
|
<Label for={field.id} class={field.class}>{field.label}</Label>
|
|
246
245
|
{#key field.id}
|
|
247
246
|
<Editor initialValue={field.value} bind:html={field.value} />
|
|
248
|
-
<input type="hidden" name={field.id} bind:value={field.value} />
|
|
249
247
|
{/key}
|
|
250
248
|
{/snippet}
|
|
251
249
|
|