uncial 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +228 -0
- package/dist/core/attributes.d.ts +11 -0
- package/dist/core/attributes.js +149 -0
- package/dist/core/defineBlock.d.ts +2 -0
- package/dist/core/defineBlock.js +77 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.js +6 -0
- package/dist/core/normalize.d.ts +5 -0
- package/dist/core/normalize.js +38 -0
- package/dist/core/registry.d.ts +4 -0
- package/dist/core/registry.js +41 -0
- package/dist/core/types.d.ts +130 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validate.d.ts +4 -0
- package/dist/core/validate.js +181 -0
- package/dist/editor/BlockAttributesPanel.svelte +354 -0
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -0
- package/dist/editor/BlockMenu.svelte +63 -0
- package/dist/editor/BlockMenu.svelte.d.ts +24 -0
- package/dist/editor/BlockNodeView.svelte +88 -0
- package/dist/editor/BlockNodeView.svelte.d.ts +14 -0
- package/dist/editor/Editor.svelte +453 -0
- package/dist/editor/Editor.svelte.d.ts +17 -0
- package/dist/editor/LinkAttributesPanel.svelte +96 -0
- package/dist/editor/LinkAttributesPanel.svelte.d.ts +17 -0
- package/dist/editor/RichTextAttributeEditor.svelte +232 -0
- package/dist/editor/RichTextAttributeEditor.svelte.d.ts +11 -0
- package/dist/editor/Toolbar.svelte +124 -0
- package/dist/editor/Toolbar.svelte.d.ts +14 -0
- package/dist/editor/attributesController.d.ts +64 -0
- package/dist/editor/attributesController.js +608 -0
- package/dist/editor/bindEditor.d.ts +15 -0
- package/dist/editor/bindEditor.js +159 -0
- package/dist/editor/createEditorExtensions.d.ts +1 -0
- package/dist/editor/createEditorExtensions.js +1 -0
- package/dist/editor/index.d.ts +11 -0
- package/dist/editor/index.js +8 -0
- package/dist/editor/toolbarFeatures.d.ts +26 -0
- package/dist/editor/toolbarFeatures.js +144 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/render/ContainerBlockFixture.svelte +22 -0
- package/dist/render/ContainerBlockFixture.svelte.d.ts +10 -0
- package/dist/render/Renderer.svelte +40 -0
- package/dist/render/Renderer.svelte.d.ts +11 -0
- package/dist/render/RichContent.svelte +30 -0
- package/dist/render/RichContent.svelte.d.ts +10 -0
- package/dist/render/RichNode.svelte +105 -0
- package/dist/render/RichNode.svelte.d.ts +10 -0
- package/dist/render/RichText.svelte +29 -0
- package/dist/render/RichText.svelte.d.ts +8 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.js +2 -0
- package/dist/render/sanitize.d.ts +1 -0
- package/dist/render/sanitize.js +17 -0
- package/dist/shared/codeBlockAttributes.d.ts +31 -0
- package/dist/shared/codeBlockAttributes.js +25 -0
- package/dist/shared/content.d.ts +2 -0
- package/dist/shared/content.js +7 -0
- package/dist/shared/document.d.ts +18 -0
- package/dist/shared/document.js +1 -0
- package/dist/shared/richText.d.ts +11 -0
- package/dist/shared/richText.js +108 -0
- package/dist/shared/syntaxHighlight.d.ts +16 -0
- package/dist/shared/syntaxHighlight.js +47 -0
- package/dist/shared/tiptap.d.ts +7 -0
- package/dist/shared/tiptap.js +259 -0
- package/package.json +116 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Editor as TiptapEditor } from '@tiptap/core';
|
|
2
|
+
import { normalizeDocument } from '../core/normalize.js';
|
|
3
|
+
import { validateDocument } from '../core/validate.js';
|
|
4
|
+
import { resolveRegistry } from '../core/registry.js';
|
|
5
|
+
import { emptyDocument } from '../shared/content.js';
|
|
6
|
+
import { createEditorExtensions } from '../shared/tiptap.js';
|
|
7
|
+
import { createBlockAttributesController } from './attributesController.js';
|
|
8
|
+
function toEditorDocument(document) {
|
|
9
|
+
const { version: _version, ...editorDocument } = document;
|
|
10
|
+
void _version;
|
|
11
|
+
return editorDocument;
|
|
12
|
+
}
|
|
13
|
+
function createInitialControllerState() {
|
|
14
|
+
return {
|
|
15
|
+
open: false,
|
|
16
|
+
mode: null,
|
|
17
|
+
selectedBlockId: '',
|
|
18
|
+
draftAttrs: {},
|
|
19
|
+
validationErrors: {},
|
|
20
|
+
activeBlock: null,
|
|
21
|
+
allowedBlockIds: [],
|
|
22
|
+
containerChildren: [],
|
|
23
|
+
link: {
|
|
24
|
+
open: false,
|
|
25
|
+
attrs: {}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function bindEditor(node, initialOptions = {}) {
|
|
30
|
+
const internalController = createBlockAttributesController();
|
|
31
|
+
let options = initialOptions;
|
|
32
|
+
let registry = resolveRegistry(options.blocks ?? []);
|
|
33
|
+
let schema = options.schema;
|
|
34
|
+
let controller = options.attributesController ?? internalController;
|
|
35
|
+
let controllerState = createInitialControllerState();
|
|
36
|
+
let unsubscribe = controller.subscribe((state) => {
|
|
37
|
+
controllerState = state;
|
|
38
|
+
});
|
|
39
|
+
let editor = null;
|
|
40
|
+
let lastSerialized = '';
|
|
41
|
+
function normalizeAndValidate(document) {
|
|
42
|
+
const normalized = normalizeDocument(document, registry, schema);
|
|
43
|
+
if (schema) {
|
|
44
|
+
validateDocument(normalized, registry, schema, { onIssue: options.onIssue });
|
|
45
|
+
}
|
|
46
|
+
return normalized;
|
|
47
|
+
}
|
|
48
|
+
function setController(nextController) {
|
|
49
|
+
if (controller === nextController)
|
|
50
|
+
return;
|
|
51
|
+
unsubscribe();
|
|
52
|
+
controller.detach();
|
|
53
|
+
controller = nextController;
|
|
54
|
+
unsubscribe = controller.subscribe((state) => {
|
|
55
|
+
controllerState = state;
|
|
56
|
+
});
|
|
57
|
+
if (editor) {
|
|
58
|
+
controller.attach(editor, registry, schema);
|
|
59
|
+
controller.syncFromSelection('selection');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function shouldAutoOpenAttributesForSelection(nextEditor) {
|
|
63
|
+
if (controller.isSelectionAutoOpenSuppressed()) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const selection = nextEditor.state.selection;
|
|
67
|
+
const selectedNode = selection.node;
|
|
68
|
+
const selectedBlockId = selectedNode?.type?.name;
|
|
69
|
+
if (!selectedBlockId) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const block = registry.get(selectedBlockId);
|
|
73
|
+
if (!block || block.content) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return !controllerState.open || controllerState.selectedBlockId !== selectedBlockId;
|
|
77
|
+
}
|
|
78
|
+
function setExternalDocument(nextDocument) {
|
|
79
|
+
const serialized = JSON.stringify(nextDocument);
|
|
80
|
+
if (serialized !== JSON.stringify(options.json ?? emptyDocument())) {
|
|
81
|
+
options.onChange?.(nextDocument);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function createEditor() {
|
|
85
|
+
const initialContent = normalizeAndValidate(options.json ?? emptyDocument());
|
|
86
|
+
const initialEditorContent = toEditorDocument(initialContent);
|
|
87
|
+
lastSerialized = JSON.stringify(initialEditorContent);
|
|
88
|
+
setExternalDocument(initialContent);
|
|
89
|
+
editor = new TiptapEditor({
|
|
90
|
+
element: node,
|
|
91
|
+
extensions: createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? []),
|
|
92
|
+
content: initialEditorContent,
|
|
93
|
+
onUpdate: ({ editor: tiptap }) => {
|
|
94
|
+
const raw = tiptap.getJSON();
|
|
95
|
+
const normalized = normalizeAndValidate(raw);
|
|
96
|
+
lastSerialized = JSON.stringify(toEditorDocument(normalized));
|
|
97
|
+
setExternalDocument(normalized);
|
|
98
|
+
controller.syncFromSelection('update');
|
|
99
|
+
},
|
|
100
|
+
onSelectionUpdate: ({ editor: tiptap }) => {
|
|
101
|
+
controller.syncFromSelection('selection');
|
|
102
|
+
if (shouldAutoOpenAttributesForSelection(tiptap)) {
|
|
103
|
+
controller.openAttributes();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
options.onEditor?.(editor);
|
|
108
|
+
controller.attach(editor, registry, schema);
|
|
109
|
+
controller.syncFromSelection('selection');
|
|
110
|
+
}
|
|
111
|
+
function destroyEditor() {
|
|
112
|
+
controller.detach();
|
|
113
|
+
editor?.destroy();
|
|
114
|
+
editor = null;
|
|
115
|
+
options.onEditor?.(null);
|
|
116
|
+
}
|
|
117
|
+
function syncEditorDocument(nextDocument) {
|
|
118
|
+
if (!editor)
|
|
119
|
+
return;
|
|
120
|
+
const editorDocument = toEditorDocument(nextDocument);
|
|
121
|
+
const serialized = JSON.stringify(editorDocument);
|
|
122
|
+
if (serialized === lastSerialized)
|
|
123
|
+
return;
|
|
124
|
+
editor.commands.setContent(editorDocument, { emitUpdate: false });
|
|
125
|
+
lastSerialized = serialized;
|
|
126
|
+
controller.syncFromSelection('update');
|
|
127
|
+
}
|
|
128
|
+
function applyOptions(nextOptions, forceRecreate = false) {
|
|
129
|
+
const previousBlocks = options.blocks;
|
|
130
|
+
const previousSchema = options.schema;
|
|
131
|
+
const previousExtensions = options.extensions;
|
|
132
|
+
options = nextOptions;
|
|
133
|
+
registry = resolveRegistry(options.blocks ?? []);
|
|
134
|
+
schema = options.schema;
|
|
135
|
+
setController(options.attributesController ?? internalController);
|
|
136
|
+
const shouldRecreate = forceRecreate ||
|
|
137
|
+
previousBlocks !== options.blocks ||
|
|
138
|
+
previousSchema !== options.schema ||
|
|
139
|
+
previousExtensions !== options.extensions;
|
|
140
|
+
if (shouldRecreate) {
|
|
141
|
+
destroyEditor();
|
|
142
|
+
createEditor();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const normalized = normalizeAndValidate(options.json ?? emptyDocument());
|
|
146
|
+
setExternalDocument(normalized);
|
|
147
|
+
syncEditorDocument(normalized);
|
|
148
|
+
}
|
|
149
|
+
createEditor();
|
|
150
|
+
return {
|
|
151
|
+
update(nextOptions) {
|
|
152
|
+
applyOptions(nextOptions);
|
|
153
|
+
},
|
|
154
|
+
destroy() {
|
|
155
|
+
unsubscribe();
|
|
156
|
+
destroyEditor();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEditorExtensions } from '../shared/tiptap.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEditorExtensions } from '../shared/tiptap.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as Editor } from './Editor.svelte';
|
|
2
|
+
export { default as BlockAttributesPanel } from './BlockAttributesPanel.svelte';
|
|
3
|
+
export { default as Toolbar } from './Toolbar.svelte';
|
|
4
|
+
export { default as BlockMenu } from './BlockMenu.svelte';
|
|
5
|
+
export { bindEditor } from './bindEditor.js';
|
|
6
|
+
export { createEditorExtensions } from './createEditorExtensions.js';
|
|
7
|
+
export { createBlockAttributesController } from './attributesController.js';
|
|
8
|
+
export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
|
|
9
|
+
export type { BindEditorOptions } from './bindEditor.js';
|
|
10
|
+
export type { ToolbarFeature, ToolbarFeatureContext, ToolbarFeatureId, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
|
11
|
+
export type { ActiveBlockSelection, BlockAttributeMode, BlockAttributesController, BlockAttributesState, ContainerChildInfo, LinkAttributes, LinkAttributesState } from './attributesController.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { default as Editor } from './Editor.svelte';
|
|
2
|
+
export { default as BlockAttributesPanel } from './BlockAttributesPanel.svelte';
|
|
3
|
+
export { default as Toolbar } from './Toolbar.svelte';
|
|
4
|
+
export { default as BlockMenu } from './BlockMenu.svelte';
|
|
5
|
+
export { bindEditor } from './bindEditor.js';
|
|
6
|
+
export { createEditorExtensions } from './createEditorExtensions.js';
|
|
7
|
+
export { createBlockAttributesController } from './attributesController.js';
|
|
8
|
+
export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
import type { ContentSchema } from '../core/types.js';
|
|
3
|
+
export type ToolbarFeatureId = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'heading2' | 'heading3' | 'heading4' | 'heading5' | 'heading6' | 'bulletList' | 'orderedList' | 'blockquote' | 'codeBlock' | 'horizontalRule';
|
|
4
|
+
export type ToolbarFeatureSelection = '*' | '__all__' | readonly string[];
|
|
5
|
+
export interface ToolbarFeatureContext {
|
|
6
|
+
editor: Editor;
|
|
7
|
+
schema?: ContentSchema;
|
|
8
|
+
}
|
|
9
|
+
export interface ToolbarFeature {
|
|
10
|
+
id: string;
|
|
11
|
+
label: string;
|
|
12
|
+
tooltip?: string;
|
|
13
|
+
group?: 'mark' | 'heading' | 'block' | 'insert' | string;
|
|
14
|
+
isAllowed?: (context: ToolbarFeatureContext) => boolean;
|
|
15
|
+
isActive?: (context: ToolbarFeatureContext) => boolean;
|
|
16
|
+
canRun?: (context: ToolbarFeatureContext) => boolean;
|
|
17
|
+
run: (context: ToolbarFeatureContext) => void;
|
|
18
|
+
}
|
|
19
|
+
export declare const defaultToolbarFeatures: readonly ToolbarFeatureId[];
|
|
20
|
+
export declare const builtinToolbarFeatures: readonly ToolbarFeature[];
|
|
21
|
+
export declare function resolveToolbarFeatures({ editor, schema, toolbarFeatures, toolbarExtensions }: {
|
|
22
|
+
editor: Editor | null | undefined;
|
|
23
|
+
schema?: ContentSchema;
|
|
24
|
+
toolbarFeatures?: ToolbarFeatureSelection;
|
|
25
|
+
toolbarExtensions?: readonly ToolbarFeature[];
|
|
26
|
+
}): ToolbarFeature[];
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export const defaultToolbarFeatures = [
|
|
2
|
+
'bold',
|
|
3
|
+
'italic',
|
|
4
|
+
'strike',
|
|
5
|
+
'code',
|
|
6
|
+
'link',
|
|
7
|
+
'heading2',
|
|
8
|
+
'heading3',
|
|
9
|
+
'heading4',
|
|
10
|
+
'heading5',
|
|
11
|
+
'heading6',
|
|
12
|
+
'bulletList',
|
|
13
|
+
'orderedList',
|
|
14
|
+
'blockquote',
|
|
15
|
+
'codeBlock',
|
|
16
|
+
'horizontalRule'
|
|
17
|
+
];
|
|
18
|
+
function markAllowed(mark) {
|
|
19
|
+
return ({ schema }) => !schema || schema.allowedMarks.has(mark);
|
|
20
|
+
}
|
|
21
|
+
function activeMark(mark) {
|
|
22
|
+
return ({ editor }) => editor.isActive(mark);
|
|
23
|
+
}
|
|
24
|
+
function toggleMark(command) {
|
|
25
|
+
return ({ editor }) => editor.chain().focus()[command]().run();
|
|
26
|
+
}
|
|
27
|
+
function canToggleMark(command) {
|
|
28
|
+
return ({ editor }) => editor.can().chain().focus()[command]().run();
|
|
29
|
+
}
|
|
30
|
+
function headingFeature(level) {
|
|
31
|
+
return {
|
|
32
|
+
id: `heading${level}`,
|
|
33
|
+
label: `H${level}`,
|
|
34
|
+
tooltip: `Heading ${level}`,
|
|
35
|
+
group: 'heading',
|
|
36
|
+
isActive: ({ editor }) => editor.isActive('heading', { level }),
|
|
37
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleHeading({ level }).run(),
|
|
38
|
+
run: ({ editor }) => editor.chain().focus().toggleHeading({ level }).run()
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export const builtinToolbarFeatures = [
|
|
42
|
+
{
|
|
43
|
+
id: 'bold',
|
|
44
|
+
label: 'Bold',
|
|
45
|
+
group: 'mark',
|
|
46
|
+
isAllowed: markAllowed('bold'),
|
|
47
|
+
isActive: activeMark('bold'),
|
|
48
|
+
canRun: canToggleMark('toggleBold'),
|
|
49
|
+
run: toggleMark('toggleBold')
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: 'italic',
|
|
53
|
+
label: 'Italic',
|
|
54
|
+
group: 'mark',
|
|
55
|
+
isAllowed: markAllowed('italic'),
|
|
56
|
+
isActive: activeMark('italic'),
|
|
57
|
+
canRun: canToggleMark('toggleItalic'),
|
|
58
|
+
run: toggleMark('toggleItalic')
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'strike',
|
|
62
|
+
label: 'Strike',
|
|
63
|
+
group: 'mark',
|
|
64
|
+
isAllowed: markAllowed('strike'),
|
|
65
|
+
isActive: activeMark('strike'),
|
|
66
|
+
canRun: canToggleMark('toggleStrike'),
|
|
67
|
+
run: toggleMark('toggleStrike')
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: 'code',
|
|
71
|
+
label: 'Code',
|
|
72
|
+
group: 'mark',
|
|
73
|
+
isAllowed: markAllowed('code'),
|
|
74
|
+
isActive: activeMark('code'),
|
|
75
|
+
canRun: canToggleMark('toggleCode'),
|
|
76
|
+
run: toggleMark('toggleCode')
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: 'link',
|
|
80
|
+
label: 'Link',
|
|
81
|
+
group: 'mark',
|
|
82
|
+
isAllowed: markAllowed('link'),
|
|
83
|
+
isActive: activeMark('link'),
|
|
84
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleLink({ href: '#' }).run(),
|
|
85
|
+
run: ({ editor }) => editor.chain().focus().toggleLink({ href: '#' }).run()
|
|
86
|
+
},
|
|
87
|
+
headingFeature(2),
|
|
88
|
+
headingFeature(3),
|
|
89
|
+
headingFeature(4),
|
|
90
|
+
headingFeature(5),
|
|
91
|
+
headingFeature(6),
|
|
92
|
+
{
|
|
93
|
+
id: 'bulletList',
|
|
94
|
+
label: 'Bullet list',
|
|
95
|
+
group: 'block',
|
|
96
|
+
isActive: ({ editor }) => editor.isActive('bulletList'),
|
|
97
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleBulletList().run(),
|
|
98
|
+
run: ({ editor }) => editor.chain().focus().toggleBulletList().run()
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: 'orderedList',
|
|
102
|
+
label: 'Ordered list',
|
|
103
|
+
group: 'block',
|
|
104
|
+
isActive: ({ editor }) => editor.isActive('orderedList'),
|
|
105
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleOrderedList().run(),
|
|
106
|
+
run: ({ editor }) => editor.chain().focus().toggleOrderedList().run()
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
id: 'blockquote',
|
|
110
|
+
label: 'Blockquote',
|
|
111
|
+
group: 'block',
|
|
112
|
+
isActive: ({ editor }) => editor.isActive('blockquote'),
|
|
113
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleBlockquote().run(),
|
|
114
|
+
run: ({ editor }) => editor.chain().focus().toggleBlockquote().run()
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'codeBlock',
|
|
118
|
+
label: 'Code block',
|
|
119
|
+
group: 'block',
|
|
120
|
+
isActive: ({ editor }) => editor.isActive('codeBlock'),
|
|
121
|
+
canRun: ({ editor }) => editor.can().chain().focus().toggleCodeBlock().run(),
|
|
122
|
+
run: ({ editor }) => editor.chain().focus().toggleCodeBlock().run()
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
id: 'horizontalRule',
|
|
126
|
+
label: 'Horizontal rule',
|
|
127
|
+
group: 'block',
|
|
128
|
+
canRun: ({ editor }) => editor.can().chain().focus().setHorizontalRule().run(),
|
|
129
|
+
run: ({ editor }) => editor.chain().focus().setHorizontalRule().run()
|
|
130
|
+
}
|
|
131
|
+
];
|
|
132
|
+
export function resolveToolbarFeatures({ editor, schema, toolbarFeatures, toolbarExtensions = [] }) {
|
|
133
|
+
if (!editor)
|
|
134
|
+
return [];
|
|
135
|
+
const descriptors = [...builtinToolbarFeatures, ...toolbarExtensions];
|
|
136
|
+
const selection = toolbarFeatures ?? defaultToolbarFeatures;
|
|
137
|
+
const selectedIds = selection === '*' || selection === '__all__' ? null : new Set(selection);
|
|
138
|
+
const context = { editor, schema };
|
|
139
|
+
return descriptors.filter((feature) => {
|
|
140
|
+
if (selectedIds && !selectedIds.has(feature.id))
|
|
141
|
+
return false;
|
|
142
|
+
return feature.isAllowed?.(context) ?? true;
|
|
143
|
+
});
|
|
144
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { PMNode } from '../shared/document.js';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
title?: string;
|
|
7
|
+
content?: PMNode[];
|
|
8
|
+
children?: Snippet;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let { title = '', content = [], children }: Props = $props();
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<section data-testid="container-block">
|
|
15
|
+
<h2>{title}</h2>
|
|
16
|
+
<p data-testid="child-count">{content.length}</p>
|
|
17
|
+
<div data-testid="container-children">
|
|
18
|
+
{#if children}
|
|
19
|
+
{@render children()}
|
|
20
|
+
{/if}
|
|
21
|
+
</div>
|
|
22
|
+
</section>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PMNode } from '../shared/document.js';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
interface Props {
|
|
4
|
+
title?: string;
|
|
5
|
+
content?: PMNode[];
|
|
6
|
+
children?: Snippet;
|
|
7
|
+
}
|
|
8
|
+
declare const ContainerBlockFixture: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type ContainerBlockFixture = ReturnType<typeof ContainerBlockFixture>;
|
|
10
|
+
export default ContainerBlockFixture;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { JSONContent } from '@tiptap/core';
|
|
3
|
+
import { normalizeDocument } from '../core/normalize.js';
|
|
4
|
+
import { validateDocument } from '../core/validate.js';
|
|
5
|
+
import type {
|
|
6
|
+
BlockDefinition,
|
|
7
|
+
BlockRegistry,
|
|
8
|
+
ContentSchema,
|
|
9
|
+
ValidationIssue
|
|
10
|
+
} from '../core/types.js';
|
|
11
|
+
import type { PMDoc, PMNode } from '../shared/document.js';
|
|
12
|
+
import { resolveRegistry } from '../core/registry.js';
|
|
13
|
+
import { emptyDocument } from '../shared/content.js';
|
|
14
|
+
import RichContent from './RichContent.svelte';
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
content?: JSONContent;
|
|
18
|
+
blocks?: BlockRegistry | BlockDefinition[];
|
|
19
|
+
schema?: ContentSchema;
|
|
20
|
+
onIssue?: (issue: ValidationIssue) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let { content = emptyDocument(), blocks = [], schema = undefined, onIssue }: Props = $props();
|
|
24
|
+
|
|
25
|
+
const registry = $derived(resolveRegistry(blocks));
|
|
26
|
+
const normalizedContent = $derived(
|
|
27
|
+
normalizeDocument(content as Partial<PMDoc>, registry, schema)
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
$effect(() => {
|
|
31
|
+
if (!schema) return;
|
|
32
|
+
validateDocument(normalizedContent, registry, schema, { onIssue });
|
|
33
|
+
});
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<div class="uncial-renderer rounded-box border border-base-300 bg-base-100 shadow-sm">
|
|
37
|
+
<div class="uncial-content uncial-rich-content min-h-[28rem] p-5 leading-7 sm:p-8">
|
|
38
|
+
<RichContent nodes={(normalizedContent.content ?? []) as PMNode[]} {registry} {schema} />
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JSONContent } from '@tiptap/core';
|
|
2
|
+
import type { BlockDefinition, BlockRegistry, ContentSchema, ValidationIssue } from '../core/types.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
content?: JSONContent;
|
|
5
|
+
blocks?: BlockRegistry | BlockDefinition[];
|
|
6
|
+
schema?: ContentSchema;
|
|
7
|
+
onIssue?: (issue: ValidationIssue) => void;
|
|
8
|
+
}
|
|
9
|
+
declare const Renderer: import("svelte").Component<Props, {}, "">;
|
|
10
|
+
type Renderer = ReturnType<typeof Renderer>;
|
|
11
|
+
export default Renderer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { BlockRegistry, ContentSchema } from '../core/types.js';
|
|
3
|
+
import type { PMNode } from '../shared/document.js';
|
|
4
|
+
import RichNode from './RichNode.svelte';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
nodes?: PMNode[];
|
|
8
|
+
registry: BlockRegistry;
|
|
9
|
+
schema?: ContentSchema;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { nodes = [], registry, schema = undefined }: Props = $props();
|
|
13
|
+
|
|
14
|
+
function getNodeKey(node: PMNode, index: number): string {
|
|
15
|
+
const id = node.attrs?.id;
|
|
16
|
+
if (typeof id === 'string' || typeof id === 'number') {
|
|
17
|
+
return `${node.type}:${id}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof node.text === 'string' && node.text.length > 0) {
|
|
21
|
+
return `${node.type}:${node.text}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return `${node.type}:${JSON.stringify(node.attrs ?? {})}:${index}`;
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
{#each nodes as node, index (getNodeKey(node, index))}
|
|
29
|
+
<RichNode {node} {registry} {schema} />
|
|
30
|
+
{/each}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BlockRegistry, ContentSchema } from '../core/types.js';
|
|
2
|
+
import type { PMNode } from '../shared/document.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
nodes?: PMNode[];
|
|
5
|
+
registry: BlockRegistry;
|
|
6
|
+
schema?: ContentSchema;
|
|
7
|
+
}
|
|
8
|
+
declare const RichContent: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type RichContent = ReturnType<typeof RichContent>;
|
|
10
|
+
export default RichContent;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { BlockRegistry, ContentSchema } from '../core/types.js';
|
|
3
|
+
import type { PMMark, PMNode } from '../shared/document.js';
|
|
4
|
+
import { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
|
|
5
|
+
import RichContent from './RichContent.svelte';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
node: PMNode;
|
|
9
|
+
registry: BlockRegistry;
|
|
10
|
+
schema?: ContentSchema;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let { node, registry, schema = undefined }: Props = $props();
|
|
14
|
+
|
|
15
|
+
const block = $derived(registry.get(node.type));
|
|
16
|
+
const blockAttrs = $derived((node.attrs ?? {}) as Record<string, unknown>);
|
|
17
|
+
const blockContent = $derived((node.content ?? []) as PMNode[]);
|
|
18
|
+
const codeLanguage = $derived(node.attrs?.language);
|
|
19
|
+
const codeText = $derived.by(() =>
|
|
20
|
+
(node.content ?? [])
|
|
21
|
+
.filter((child) => child.type === 'text')
|
|
22
|
+
.map((child) => child.text ?? '')
|
|
23
|
+
.join('')
|
|
24
|
+
);
|
|
25
|
+
const highlightedCode = $derived(highlightCodeToHtml(codeText, codeLanguage));
|
|
26
|
+
|
|
27
|
+
function getActiveMarks(marks: PMMark[] = []): PMMark[] {
|
|
28
|
+
return schema ? marks.filter((mark) => schema.allowedMarks.has(mark.type)) : marks;
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
{#snippet blockChildren()}
|
|
33
|
+
<RichContent nodes={blockContent} {registry} {schema} />
|
|
34
|
+
{/snippet}
|
|
35
|
+
|
|
36
|
+
{#snippet renderMarkedText(text: string, marks: PMMark[])}
|
|
37
|
+
{#if marks.length === 0}
|
|
38
|
+
{text}
|
|
39
|
+
{:else}
|
|
40
|
+
{@const [mark, ...rest] = marks}
|
|
41
|
+
{#if mark.type === 'bold'}
|
|
42
|
+
<strong>{@render renderMarkedText(text, rest)}</strong>
|
|
43
|
+
{:else if mark.type === 'italic'}
|
|
44
|
+
<em>{@render renderMarkedText(text, rest)}</em>
|
|
45
|
+
{:else if mark.type === 'strike'}
|
|
46
|
+
<s>{@render renderMarkedText(text, rest)}</s>
|
|
47
|
+
{:else if mark.type === 'code'}
|
|
48
|
+
<code>{@render renderMarkedText(text, rest)}</code>
|
|
49
|
+
{:else}
|
|
50
|
+
{@render renderMarkedText(text, rest)}
|
|
51
|
+
{/if}
|
|
52
|
+
{/if}
|
|
53
|
+
{/snippet}
|
|
54
|
+
|
|
55
|
+
{#if node.type === 'codeBlock'}
|
|
56
|
+
<!-- eslint-disable svelte/no-at-html-tags -- highlighted code is escaped in syntaxHighlight.ts -->
|
|
57
|
+
<pre class="uncial-code-block"><code class={getCodeLanguageClass(codeLanguage)}
|
|
58
|
+
>{@html highlightedCode}</code
|
|
59
|
+
></pre>
|
|
60
|
+
<!-- eslint-enable svelte/no-at-html-tags -->
|
|
61
|
+
{:else if block && (!schema || schema.allowedBlocks.has(block.id))}
|
|
62
|
+
{@const RenderComponent = block.components.render}
|
|
63
|
+
<RenderComponent {...blockAttrs} content={blockContent} children={block.content ? blockChildren : undefined} />
|
|
64
|
+
{:else if node.type === 'paragraph'}
|
|
65
|
+
<p>
|
|
66
|
+
<RichContent nodes={node.content ?? []} {registry} {schema} />
|
|
67
|
+
</p>
|
|
68
|
+
{:else if node.type === 'heading'}
|
|
69
|
+
{@const level = Math.min(6, Math.max(1, Number(node.attrs?.level ?? 1)))}
|
|
70
|
+
{#if level === 1}
|
|
71
|
+
<h1><RichContent nodes={node.content ?? []} {registry} {schema} /></h1>
|
|
72
|
+
{:else if level === 2}
|
|
73
|
+
<h2><RichContent nodes={node.content ?? []} {registry} {schema} /></h2>
|
|
74
|
+
{:else if level === 3}
|
|
75
|
+
<h3><RichContent nodes={node.content ?? []} {registry} {schema} /></h3>
|
|
76
|
+
{:else if level === 4}
|
|
77
|
+
<h4><RichContent nodes={node.content ?? []} {registry} {schema} /></h4>
|
|
78
|
+
{:else if level === 5}
|
|
79
|
+
<h5><RichContent nodes={node.content ?? []} {registry} {schema} /></h5>
|
|
80
|
+
{:else}
|
|
81
|
+
<h6><RichContent nodes={node.content ?? []} {registry} {schema} /></h6>
|
|
82
|
+
{/if}
|
|
83
|
+
{:else if node.type === 'bulletList'}
|
|
84
|
+
<ul>
|
|
85
|
+
<RichContent nodes={node.content ?? []} {registry} {schema} />
|
|
86
|
+
</ul>
|
|
87
|
+
{:else if node.type === 'orderedList'}
|
|
88
|
+
<ol>
|
|
89
|
+
<RichContent nodes={node.content ?? []} {registry} {schema} />
|
|
90
|
+
</ol>
|
|
91
|
+
{:else if node.type === 'listItem'}
|
|
92
|
+
<li>
|
|
93
|
+
<RichContent nodes={node.content ?? []} {registry} {schema} />
|
|
94
|
+
</li>
|
|
95
|
+
{:else if node.type === 'blockquote'}
|
|
96
|
+
<blockquote>
|
|
97
|
+
<RichContent nodes={node.content ?? []} {registry} {schema} />
|
|
98
|
+
</blockquote>
|
|
99
|
+
{:else if node.type === 'horizontalRule'}
|
|
100
|
+
<hr />
|
|
101
|
+
{:else if node.type === 'hardBreak'}
|
|
102
|
+
<br />
|
|
103
|
+
{:else if node.type === 'text'}
|
|
104
|
+
{@render renderMarkedText(node.text ?? '', getActiveMarks(node.marks ?? []))}
|
|
105
|
+
{/if}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BlockRegistry, ContentSchema } from '../core/types.js';
|
|
2
|
+
import type { PMNode } from '../shared/document.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
node: PMNode;
|
|
5
|
+
registry: BlockRegistry;
|
|
6
|
+
schema?: ContentSchema;
|
|
7
|
+
}
|
|
8
|
+
declare const RichNode: import("svelte").Component<Props, {}, "">;
|
|
9
|
+
type RichNode = ReturnType<typeof RichNode>;
|
|
10
|
+
export default RichNode;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createBlockRegistry } from '../core/registry.js';
|
|
3
|
+
import type { ContentSchema, RichTextFeatureSelection } from '../core/types.js';
|
|
4
|
+
import type { PMNode } from '../shared/document.js';
|
|
5
|
+
import {
|
|
6
|
+
coerceRichTextDocument,
|
|
7
|
+
filterRichTextNodes,
|
|
8
|
+
resolveRichTextFeatures
|
|
9
|
+
} from '../shared/richText.js';
|
|
10
|
+
import RichContent from './RichContent.svelte';
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
content?: unknown;
|
|
14
|
+
features?: RichTextFeatureSelection;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let { content = undefined, features = undefined }: Props = $props();
|
|
18
|
+
|
|
19
|
+
const registry = createBlockRegistry([]);
|
|
20
|
+
const resolvedFeatures = $derived(resolveRichTextFeatures(features));
|
|
21
|
+
const document = $derived(coerceRichTextDocument(content));
|
|
22
|
+
const nodes = $derived(filterRichTextNodes(document.content ?? [], resolvedFeatures));
|
|
23
|
+
const schema = $derived<ContentSchema>({
|
|
24
|
+
allowedBlocks: new Set(),
|
|
25
|
+
allowedMarks: resolvedFeatures
|
|
26
|
+
});
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<RichContent nodes={nodes as PMNode[]} {registry} {schema} />
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RichTextFeatureSelection } from '../core/types.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
content?: unknown;
|
|
4
|
+
features?: RichTextFeatureSelection;
|
|
5
|
+
}
|
|
6
|
+
declare const RichText: import("svelte").Component<Props, {}, "">;
|
|
7
|
+
type RichText = ReturnType<typeof RichText>;
|
|
8
|
+
export default RichText;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sanitizeHref(value: unknown): string | null;
|