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,17 @@
|
|
|
1
|
+
const SAFE_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:']);
|
|
2
|
+
export function sanitizeHref(value) {
|
|
3
|
+
if (typeof value !== 'string')
|
|
4
|
+
return null;
|
|
5
|
+
const trimmed = value.trim();
|
|
6
|
+
if (!trimmed)
|
|
7
|
+
return null;
|
|
8
|
+
if (trimmed.startsWith('/') || trimmed.startsWith('#'))
|
|
9
|
+
return trimmed;
|
|
10
|
+
try {
|
|
11
|
+
const url = new URL(trimmed, 'https://uncial.local');
|
|
12
|
+
return SAFE_PROTOCOLS.has(url.protocol) ? trimmed : null;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const CODE_BLOCK_ID = "codeBlock";
|
|
2
|
+
export declare const CODE_BLOCK_LANGUAGE_OPTIONS: {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
}[];
|
|
6
|
+
export declare const codeBlockAttributes: {
|
|
7
|
+
language: {
|
|
8
|
+
default: string;
|
|
9
|
+
input: "select";
|
|
10
|
+
options: {
|
|
11
|
+
value: string;
|
|
12
|
+
label: string;
|
|
13
|
+
}[];
|
|
14
|
+
parse: (value: unknown) => string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare const codeBlockAttributeTarget: {
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
attributes: {
|
|
21
|
+
language: {
|
|
22
|
+
default: string;
|
|
23
|
+
input: "select";
|
|
24
|
+
options: {
|
|
25
|
+
value: string;
|
|
26
|
+
label: string;
|
|
27
|
+
}[];
|
|
28
|
+
parse: (value: unknown) => string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const CODE_BLOCK_ID = 'codeBlock';
|
|
2
|
+
export const CODE_BLOCK_LANGUAGE_OPTIONS = [
|
|
3
|
+
{ value: '', label: 'Auto' },
|
|
4
|
+
{ value: 'ts', label: 'TypeScript' },
|
|
5
|
+
{ value: 'js', label: 'JavaScript' },
|
|
6
|
+
{ value: 'svelte', label: 'Svelte' },
|
|
7
|
+
{ value: 'python', label: 'Python' },
|
|
8
|
+
{ value: 'html', label: 'HTML' },
|
|
9
|
+
{ value: 'css', label: 'CSS' },
|
|
10
|
+
{ value: 'json', label: 'JSON' },
|
|
11
|
+
{ value: 'bash', label: 'Bash' }
|
|
12
|
+
];
|
|
13
|
+
export const codeBlockAttributes = {
|
|
14
|
+
language: {
|
|
15
|
+
default: '',
|
|
16
|
+
input: 'select',
|
|
17
|
+
options: CODE_BLOCK_LANGUAGE_OPTIONS,
|
|
18
|
+
parse: (value) => (typeof value === 'string' ? value : '')
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export const codeBlockAttributeTarget = {
|
|
22
|
+
id: CODE_BLOCK_ID,
|
|
23
|
+
label: 'Code block',
|
|
24
|
+
attributes: codeBlockAttributes
|
|
25
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface PMMark {
|
|
2
|
+
type: string;
|
|
3
|
+
attrs?: Record<string, unknown>;
|
|
4
|
+
}
|
|
5
|
+
export interface PMNode {
|
|
6
|
+
type: string;
|
|
7
|
+
attrs?: Record<string, unknown>;
|
|
8
|
+
marks?: PMMark[];
|
|
9
|
+
text?: string;
|
|
10
|
+
content?: PMNode[];
|
|
11
|
+
}
|
|
12
|
+
export interface PMDoc {
|
|
13
|
+
type: 'doc';
|
|
14
|
+
version?: number;
|
|
15
|
+
content?: PMNode[];
|
|
16
|
+
}
|
|
17
|
+
export type PMPathSegment = number | 'attrs' | 'marks' | 'content' | 'text' | string;
|
|
18
|
+
export type PMPath = PMPathSegment[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RichTextFeature, RichTextFeatureSelection } from '../core/types.js';
|
|
2
|
+
import type { PMDoc, PMNode } from './document.js';
|
|
3
|
+
export declare const supportedRichTextFeatures: readonly ["bold", "italic", "strike", "code", "link", "heading", "bulletList", "orderedList", "blockquote", "codeBlock", "horizontalRule", "hardBreak"];
|
|
4
|
+
export declare const runtimeRichTextFeatures: ("bold" | "italic" | "strike" | "code" | "heading" | "bulletList" | "orderedList" | "blockquote" | "codeBlock" | "horizontalRule" | "hardBreak")[];
|
|
5
|
+
export declare function emptyRichTextDocument(): PMDoc;
|
|
6
|
+
export declare function richTextDocument(text?: string): PMDoc;
|
|
7
|
+
export declare function isRichTextDocument(value: unknown): value is PMDoc;
|
|
8
|
+
export declare function coerceRichTextDocument(value: unknown): PMDoc;
|
|
9
|
+
export declare function hasRichTextContent(value: unknown): value is PMDoc;
|
|
10
|
+
export declare function resolveRichTextFeatures(features?: RichTextFeatureSelection): ReadonlySet<RichTextFeature>;
|
|
11
|
+
export declare function filterRichTextNodes(nodes: PMNode[] | undefined, features: ReadonlySet<RichTextFeature>): PMNode[];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export const supportedRichTextFeatures = [
|
|
2
|
+
'bold',
|
|
3
|
+
'italic',
|
|
4
|
+
'strike',
|
|
5
|
+
'code',
|
|
6
|
+
'link',
|
|
7
|
+
'heading',
|
|
8
|
+
'bulletList',
|
|
9
|
+
'orderedList',
|
|
10
|
+
'blockquote',
|
|
11
|
+
'codeBlock',
|
|
12
|
+
'horizontalRule',
|
|
13
|
+
'hardBreak'
|
|
14
|
+
];
|
|
15
|
+
export const runtimeRichTextFeatures = supportedRichTextFeatures.filter((feature) => feature !== 'link');
|
|
16
|
+
const defaultRichTextFeatures = [
|
|
17
|
+
'bold',
|
|
18
|
+
'italic',
|
|
19
|
+
'bulletList',
|
|
20
|
+
'orderedList',
|
|
21
|
+
'hardBreak'
|
|
22
|
+
];
|
|
23
|
+
const supportedFeatureSet = new Set(supportedRichTextFeatures);
|
|
24
|
+
function isRecord(value) {
|
|
25
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
function isPMNode(value) {
|
|
28
|
+
if (!isRecord(value) || typeof value.type !== 'string')
|
|
29
|
+
return false;
|
|
30
|
+
if (value.content !== undefined && !Array.isArray(value.content))
|
|
31
|
+
return false;
|
|
32
|
+
if (value.marks !== undefined && !Array.isArray(value.marks))
|
|
33
|
+
return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
export function emptyRichTextDocument() {
|
|
37
|
+
return { type: 'doc', content: [] };
|
|
38
|
+
}
|
|
39
|
+
export function richTextDocument(text = '') {
|
|
40
|
+
if (!text.trim())
|
|
41
|
+
return emptyRichTextDocument();
|
|
42
|
+
return {
|
|
43
|
+
type: 'doc',
|
|
44
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text }] }]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function isRichTextDocument(value) {
|
|
48
|
+
return (isRecord(value) &&
|
|
49
|
+
value.type === 'doc' &&
|
|
50
|
+
(value.content === undefined || Array.isArray(value.content)));
|
|
51
|
+
}
|
|
52
|
+
export function coerceRichTextDocument(value) {
|
|
53
|
+
if (value === undefined || value === null || value === '')
|
|
54
|
+
return emptyRichTextDocument();
|
|
55
|
+
if (isRichTextDocument(value))
|
|
56
|
+
return value;
|
|
57
|
+
if (typeof value === 'string') {
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(value);
|
|
60
|
+
if (isRichTextDocument(parsed))
|
|
61
|
+
return parsed;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Plain strings are valid rich text input.
|
|
65
|
+
}
|
|
66
|
+
return richTextDocument(value);
|
|
67
|
+
}
|
|
68
|
+
return emptyRichTextDocument();
|
|
69
|
+
}
|
|
70
|
+
function hasNodeText(nodes = []) {
|
|
71
|
+
return nodes.some((node) => {
|
|
72
|
+
if (typeof node.text === 'string' && node.text.trim().length > 0)
|
|
73
|
+
return true;
|
|
74
|
+
return Array.isArray(node.content) && hasNodeText(node.content);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function hasRichTextContent(value) {
|
|
78
|
+
return hasNodeText(coerceRichTextDocument(value).content ?? []);
|
|
79
|
+
}
|
|
80
|
+
export function resolveRichTextFeatures(features) {
|
|
81
|
+
if (features === '*' || features === '__all__')
|
|
82
|
+
return new Set(runtimeRichTextFeatures);
|
|
83
|
+
if (!Array.isArray(features))
|
|
84
|
+
return new Set(defaultRichTextFeatures);
|
|
85
|
+
return new Set(features.filter((feature) => typeof feature === 'string' && supportedFeatureSet.has(feature) && feature !== 'link'));
|
|
86
|
+
}
|
|
87
|
+
export function filterRichTextNodes(nodes = [], features) {
|
|
88
|
+
return nodes.flatMap((node) => {
|
|
89
|
+
if (!isPMNode(node) || !isRichTextNodeAllowed(node, features))
|
|
90
|
+
return [];
|
|
91
|
+
return [
|
|
92
|
+
{
|
|
93
|
+
...node,
|
|
94
|
+
marks: node.marks?.filter((mark) => features.has(mark.type)),
|
|
95
|
+
content: node.content ? filterRichTextNodes(node.content, features) : undefined
|
|
96
|
+
}
|
|
97
|
+
];
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function isRichTextNodeAllowed(node, features) {
|
|
101
|
+
if (node.type === 'doc' ||
|
|
102
|
+
node.type === 'paragraph' ||
|
|
103
|
+
node.type === 'text' ||
|
|
104
|
+
node.type === 'listItem') {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
return features.has(node.type);
|
|
108
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const lowlight: {
|
|
2
|
+
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options> | null | undefined) => import("hast").Root;
|
|
3
|
+
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions> | null | undefined) => import("hast").Root;
|
|
4
|
+
listLanguages: () => Array<string>;
|
|
5
|
+
register: {
|
|
6
|
+
(grammars: Readonly<Record<string, import("highlight.js").LanguageFn>>): undefined;
|
|
7
|
+
(name: string, grammar: import("highlight.js").LanguageFn): undefined;
|
|
8
|
+
};
|
|
9
|
+
registerAlias: {
|
|
10
|
+
(aliases: Readonly<Record<string, ReadonlyArray<string> | string>>): undefined;
|
|
11
|
+
(language: string, alias: ReadonlyArray<string> | string): undefined;
|
|
12
|
+
};
|
|
13
|
+
registered: (aliasOrName: string) => boolean;
|
|
14
|
+
};
|
|
15
|
+
export declare function getCodeLanguageClass(language: unknown): string;
|
|
16
|
+
export declare function highlightCodeToHtml(code: string, language: unknown): string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { common, createLowlight } from 'lowlight';
|
|
2
|
+
export const lowlight = createLowlight(common);
|
|
3
|
+
lowlight.registerAlias({
|
|
4
|
+
javascript: ['js', 'jsx'],
|
|
5
|
+
python: ['py'],
|
|
6
|
+
typescript: ['ts', 'tsx'],
|
|
7
|
+
xml: ['html', 'svelte']
|
|
8
|
+
});
|
|
9
|
+
function escapeHtml(value) {
|
|
10
|
+
return value
|
|
11
|
+
.replace(/&/g, '&')
|
|
12
|
+
.replace(/</g, '<')
|
|
13
|
+
.replace(/>/g, '>')
|
|
14
|
+
.replace(/"/g, '"')
|
|
15
|
+
.replace(/'/g, ''');
|
|
16
|
+
}
|
|
17
|
+
function normalizeLanguage(language) {
|
|
18
|
+
return typeof language === 'string' && language.trim()
|
|
19
|
+
? language.trim().toLowerCase()
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
function renderHighlightedNode(node) {
|
|
23
|
+
if (node.type === 'text') {
|
|
24
|
+
return escapeHtml(node.value ?? '');
|
|
25
|
+
}
|
|
26
|
+
if (node.type !== 'element' || node.tagName !== 'span') {
|
|
27
|
+
return (node.children ?? []).map(renderHighlightedNode).join('');
|
|
28
|
+
}
|
|
29
|
+
const className = node.properties?.className;
|
|
30
|
+
const classes = Array.isArray(className)
|
|
31
|
+
? className.filter((value) => typeof value === 'string')
|
|
32
|
+
: [];
|
|
33
|
+
const attrs = classes.length ? ` class="${escapeHtml(classes.join(' '))}"` : '';
|
|
34
|
+
const children = (node.children ?? []).map(renderHighlightedNode).join('');
|
|
35
|
+
return `<span${attrs}>${children}</span>`;
|
|
36
|
+
}
|
|
37
|
+
export function getCodeLanguageClass(language) {
|
|
38
|
+
const normalized = normalizeLanguage(language);
|
|
39
|
+
return normalized ? `language-${normalized}` : '';
|
|
40
|
+
}
|
|
41
|
+
export function highlightCodeToHtml(code, language) {
|
|
42
|
+
const normalized = normalizeLanguage(language);
|
|
43
|
+
const tree = normalized && lowlight.registered(normalized)
|
|
44
|
+
? lowlight.highlight(normalized, code)
|
|
45
|
+
: lowlight.highlightAuto(code);
|
|
46
|
+
return tree.children.map(renderHighlightedNode).join('');
|
|
47
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AnyExtension } from '@tiptap/core';
|
|
2
|
+
import type { BlockDefinition, BlockRegistry, ContentSchema } from '../core/types.js';
|
|
3
|
+
import type { RichTextFeature } from '../core/types.js';
|
|
4
|
+
export type BlockActivationCallback = (pos: number) => void;
|
|
5
|
+
export declare function getBlockDefaultAttrs(block: BlockDefinition): Record<string, unknown>;
|
|
6
|
+
export declare function createRichTextExtensions(features: ReadonlySet<RichTextFeature>): AnyExtension[];
|
|
7
|
+
export declare function createEditorExtensions(blocks?: BlockRegistry | BlockDefinition[], schema?: ContentSchema, onActivateBlock?: BlockActivationCallback, extensions?: AnyExtension[]): AnyExtension[];
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
2
|
+
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
|
|
3
|
+
import { Mark, mergeAttributes, Node } from '@tiptap/core';
|
|
4
|
+
import { createRawSnippet, mount, unmount } from 'svelte';
|
|
5
|
+
import { resolveRegistry } from '../core/registry.js';
|
|
6
|
+
import { coerceAttributeValue, normalizeBlockAttributes, serializeBlockAttributes } from '../core/attributes.js';
|
|
7
|
+
import BlockNodeView from '../editor/BlockNodeView.svelte';
|
|
8
|
+
import { lowlight } from './syntaxHighlight.js';
|
|
9
|
+
function safeParseJSON(raw) {
|
|
10
|
+
if (!raw)
|
|
11
|
+
return {};
|
|
12
|
+
try {
|
|
13
|
+
const parsed = JSON.parse(raw);
|
|
14
|
+
return typeof parsed === 'object' && parsed !== null ? parsed : {};
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function getBlockDefaultAttrs(block) {
|
|
21
|
+
return normalizeBlockAttributes(block, {});
|
|
22
|
+
}
|
|
23
|
+
function mountBlockEditorComponent(block, node, container, contentDOM, mounted, onActivate, getPos) {
|
|
24
|
+
if (mounted) {
|
|
25
|
+
void unmount(mounted);
|
|
26
|
+
}
|
|
27
|
+
const host = document.createElement(block.behaviors.inline ? 'span' : 'div');
|
|
28
|
+
host.className = 'uncial-nodeview-host';
|
|
29
|
+
container.replaceChildren(host);
|
|
30
|
+
return mount(BlockNodeView, {
|
|
31
|
+
target: host,
|
|
32
|
+
props: {
|
|
33
|
+
component: block.components.editor,
|
|
34
|
+
attrs: node.attrs ?? {},
|
|
35
|
+
content: (node.content?.toJSON() ?? []),
|
|
36
|
+
blockId: block.id,
|
|
37
|
+
label: block.label,
|
|
38
|
+
draggable: block.behaviors.draggable ?? true,
|
|
39
|
+
onActivate: getPos && onActivate ? () => onActivate(getPos()) : undefined,
|
|
40
|
+
children: contentDOM
|
|
41
|
+
? createRawSnippet(() => ({
|
|
42
|
+
render: () => '<div class="uncial-nodeview-content"></div>',
|
|
43
|
+
setup: (element) => {
|
|
44
|
+
element.replaceChildren(contentDOM);
|
|
45
|
+
}
|
|
46
|
+
}))
|
|
47
|
+
: undefined
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function isDragHandleEvent(event) {
|
|
52
|
+
const target = event.target;
|
|
53
|
+
return target instanceof Element && Boolean(target.closest('[data-drag-handle]'));
|
|
54
|
+
}
|
|
55
|
+
function isInteractiveNodeViewEvent(event) {
|
|
56
|
+
const target = event.target;
|
|
57
|
+
if (!(target instanceof Element))
|
|
58
|
+
return false;
|
|
59
|
+
return Boolean(target.closest('input, textarea, select, button:not([data-drag-handle]), a[href]'));
|
|
60
|
+
}
|
|
61
|
+
function createBlockNodeView(block, node, getPos, onActivate) {
|
|
62
|
+
const dom = document.createElement(block.behaviors.inline ? 'span' : 'div');
|
|
63
|
+
dom.className = 'uncial-nodeview';
|
|
64
|
+
dom.dataset.uncialBlockId = block.id;
|
|
65
|
+
dom.draggable = block.behaviors.draggable ?? true;
|
|
66
|
+
const contentDOM = block.content
|
|
67
|
+
? document.createElement(block.behaviors.inline ? 'span' : 'div')
|
|
68
|
+
: null;
|
|
69
|
+
let lastAttrs = JSON.stringify(node.attrs ?? {});
|
|
70
|
+
function syncBlockPosition() {
|
|
71
|
+
if (!getPos)
|
|
72
|
+
return;
|
|
73
|
+
const pos = getPos();
|
|
74
|
+
dom.dataset.uncialBlockPos = String(pos);
|
|
75
|
+
}
|
|
76
|
+
if (contentDOM) {
|
|
77
|
+
contentDOM.className = 'uncial-nodeview-content';
|
|
78
|
+
}
|
|
79
|
+
let mounted = null;
|
|
80
|
+
mounted = mountBlockEditorComponent(block, node, dom, contentDOM, mounted, onActivate, getPos);
|
|
81
|
+
syncBlockPosition();
|
|
82
|
+
return {
|
|
83
|
+
dom,
|
|
84
|
+
contentDOM: contentDOM ?? undefined,
|
|
85
|
+
update(nextNode) {
|
|
86
|
+
if (nextNode.type.name !== block.id)
|
|
87
|
+
return false;
|
|
88
|
+
dom.draggable = block.behaviors.draggable ?? true;
|
|
89
|
+
syncBlockPosition();
|
|
90
|
+
const nextAttrs = JSON.stringify(nextNode.attrs ?? {});
|
|
91
|
+
if (nextAttrs === lastAttrs) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
lastAttrs = nextAttrs;
|
|
95
|
+
mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, onActivate, getPos);
|
|
96
|
+
return true;
|
|
97
|
+
},
|
|
98
|
+
stopEvent(event) {
|
|
99
|
+
if (event.type.startsWith('drag') && isDragHandleEvent(event))
|
|
100
|
+
return false;
|
|
101
|
+
return isInteractiveNodeViewEvent(event);
|
|
102
|
+
},
|
|
103
|
+
ignoreMutation(mutation) {
|
|
104
|
+
if (!contentDOM)
|
|
105
|
+
return true;
|
|
106
|
+
return !contentDOM.contains(mutation.target);
|
|
107
|
+
},
|
|
108
|
+
selectNode() {
|
|
109
|
+
dom.classList.add('ProseMirror-selectednode');
|
|
110
|
+
},
|
|
111
|
+
deselectNode() {
|
|
112
|
+
dom.classList.remove('ProseMirror-selectednode');
|
|
113
|
+
},
|
|
114
|
+
destroy() {
|
|
115
|
+
if (mounted) {
|
|
116
|
+
void unmount(mounted);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function createBlockNodeExtension(block, containerContentExpression, onActivate) {
|
|
122
|
+
const isContainer = Boolean(block.content);
|
|
123
|
+
return Node.create({
|
|
124
|
+
name: block.id,
|
|
125
|
+
group: block.behaviors.inline ? 'inline' : 'block',
|
|
126
|
+
inline: block.behaviors.inline ?? false,
|
|
127
|
+
content: isContainer ? containerContentExpression : undefined,
|
|
128
|
+
atom: !isContainer,
|
|
129
|
+
defining: isContainer,
|
|
130
|
+
isolating: isContainer,
|
|
131
|
+
draggable: block.behaviors.draggable ?? true,
|
|
132
|
+
selectable: block.behaviors.selectable ?? true,
|
|
133
|
+
addAttributes() {
|
|
134
|
+
return Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
135
|
+
name,
|
|
136
|
+
{
|
|
137
|
+
default: spec.default,
|
|
138
|
+
parseHTML: (element) => {
|
|
139
|
+
const direct = element.getAttribute(name);
|
|
140
|
+
if (direct !== null)
|
|
141
|
+
return coerceAttributeValue(spec, direct);
|
|
142
|
+
const attrs = safeParseJSON(element.getAttribute('data-uncial-attrs'));
|
|
143
|
+
return coerceAttributeValue(spec, attrs[name]);
|
|
144
|
+
},
|
|
145
|
+
renderHTML: (attributes) => {
|
|
146
|
+
const value = coerceAttributeValue(spec, attributes[name]);
|
|
147
|
+
return ['string', 'number', 'boolean'].includes(typeof value)
|
|
148
|
+
? { [name]: value }
|
|
149
|
+
: {};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
]));
|
|
153
|
+
},
|
|
154
|
+
parseHTML() {
|
|
155
|
+
return [{ tag: block.html?.parseTag ?? `div[data-uncial-block="${block.id}"]` }];
|
|
156
|
+
},
|
|
157
|
+
addNodeView() {
|
|
158
|
+
return ({ node, getPos }) => createBlockNodeView(block, node, getPos, onActivate);
|
|
159
|
+
},
|
|
160
|
+
renderHTML({ HTMLAttributes, node }) {
|
|
161
|
+
const normalizedAttrs = normalizeBlockAttributes(block, node.attrs ?? {});
|
|
162
|
+
if (block.html?.render) {
|
|
163
|
+
return block.html.render(normalizedAttrs);
|
|
164
|
+
}
|
|
165
|
+
return [
|
|
166
|
+
'div',
|
|
167
|
+
mergeAttributes(HTMLAttributes, {
|
|
168
|
+
'data-uncial-block': block.id,
|
|
169
|
+
'data-uncial-attrs': serializeBlockAttributes(block, normalizedAttrs),
|
|
170
|
+
class: 'uncial-block'
|
|
171
|
+
}),
|
|
172
|
+
isContainer ? 0 : block.label
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const LinkMark = Mark.create({
|
|
178
|
+
name: 'link',
|
|
179
|
+
inclusive: false,
|
|
180
|
+
addAttributes() {
|
|
181
|
+
return {
|
|
182
|
+
href: { default: null },
|
|
183
|
+
target: { default: null },
|
|
184
|
+
rel: { default: null }
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
parseHTML() {
|
|
188
|
+
return [{ tag: 'a[href]' }];
|
|
189
|
+
},
|
|
190
|
+
renderHTML({ HTMLAttributes }) {
|
|
191
|
+
return ['a', mergeAttributes(HTMLAttributes), 0];
|
|
192
|
+
},
|
|
193
|
+
addCommands() {
|
|
194
|
+
return {
|
|
195
|
+
toggleLink: (attributes) => ({ commands }) => Boolean(attributes?.href) &&
|
|
196
|
+
commands.toggleMark(this.name, attributes, { extendEmptyMarkRange: true }),
|
|
197
|
+
unsetLink: () => ({ commands }) => commands.unsetMark(this.name, { extendEmptyMarkRange: true })
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
function createBaseExtensions(schema, extensions = []) {
|
|
202
|
+
const marks = schema?.allowedMarks ?? new Set(['bold', 'italic', 'strike', 'code', 'link']);
|
|
203
|
+
const includeBold = marks.has('bold');
|
|
204
|
+
const includeItalic = marks.has('italic');
|
|
205
|
+
const includeStrike = marks.has('strike');
|
|
206
|
+
const includeCode = marks.has('code');
|
|
207
|
+
const includeLink = marks.has('link');
|
|
208
|
+
return [
|
|
209
|
+
StarterKit.configure({
|
|
210
|
+
bold: includeBold ? {} : false,
|
|
211
|
+
italic: includeItalic ? {} : false,
|
|
212
|
+
strike: includeStrike ? {} : false,
|
|
213
|
+
code: includeCode ? {} : false,
|
|
214
|
+
heading: { levels: [2, 3, 4, 5, 6] },
|
|
215
|
+
bulletList: {},
|
|
216
|
+
orderedList: {},
|
|
217
|
+
listItem: {},
|
|
218
|
+
blockquote: {},
|
|
219
|
+
codeBlock: false,
|
|
220
|
+
horizontalRule: {},
|
|
221
|
+
link: false
|
|
222
|
+
}),
|
|
223
|
+
CodeBlockLowlight.configure({ lowlight }),
|
|
224
|
+
...(includeLink ? [LinkMark] : []),
|
|
225
|
+
...extensions
|
|
226
|
+
];
|
|
227
|
+
}
|
|
228
|
+
export function createRichTextExtensions(features) {
|
|
229
|
+
return [
|
|
230
|
+
StarterKit.configure({
|
|
231
|
+
bold: features.has('bold') ? {} : false,
|
|
232
|
+
italic: features.has('italic') ? {} : false,
|
|
233
|
+
strike: features.has('strike') ? {} : false,
|
|
234
|
+
code: features.has('code') ? {} : false,
|
|
235
|
+
heading: features.has('heading') ? {} : false,
|
|
236
|
+
bulletList: features.has('bulletList') ? {} : false,
|
|
237
|
+
orderedList: features.has('orderedList') ? {} : false,
|
|
238
|
+
blockquote: features.has('blockquote') ? {} : false,
|
|
239
|
+
codeBlock: false,
|
|
240
|
+
horizontalRule: features.has('horizontalRule') ? {} : false,
|
|
241
|
+
hardBreak: features.has('hardBreak') ? {} : false,
|
|
242
|
+
link: false
|
|
243
|
+
}),
|
|
244
|
+
...(features.has('codeBlock') ? [CodeBlockLowlight.configure({ lowlight })] : [])
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = []) {
|
|
248
|
+
const registry = resolveRegistry(blocks);
|
|
249
|
+
const allowedBlockNames = registry.blocks
|
|
250
|
+
.filter((block) => !schema || schema.allowedBlocks.has(block.id))
|
|
251
|
+
.map((block) => block.id);
|
|
252
|
+
const containerContentExpression = allowedBlockNames.length
|
|
253
|
+
? `(${allowedBlockNames.join(' | ')})*`
|
|
254
|
+
: '';
|
|
255
|
+
const blockExtensions = registry.blocks
|
|
256
|
+
.filter((block) => !schema || schema.allowedBlocks.has(block.id))
|
|
257
|
+
.map((block) => createBlockNodeExtension(block, containerContentExpression, onActivateBlock));
|
|
258
|
+
return [...createBaseExtensions(schema), ...blockExtensions, ...extensions];
|
|
259
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "uncial",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"packageManager": "bun@1.3.10",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/d-flood/uncial.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/d-flood/uncial/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://d-flood.github.io/uncial/",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"dev": "vite dev",
|
|
16
|
+
"build": "vite build",
|
|
17
|
+
"preview": "vite preview",
|
|
18
|
+
"prepare": "bunx svelte-kit sync || true",
|
|
19
|
+
"package": "svelte-kit sync && svelte-package",
|
|
20
|
+
"prepack": "bun run package && bunx publint",
|
|
21
|
+
"changeset": "changeset",
|
|
22
|
+
"version": "changeset version",
|
|
23
|
+
"release": "bun run prepack && changeset publish",
|
|
24
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
25
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
26
|
+
"lint": "prettier --check . && eslint .",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"test:unit": "vitest --project server",
|
|
29
|
+
"test:browser": "vitest --project client",
|
|
30
|
+
"test": "bun run test:unit -- --run && bun run test:browser -- --run && bun run test:e2e",
|
|
31
|
+
"test:e2e": "playwright test"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public",
|
|
35
|
+
"provenance": true
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"!dist/**/*.test.*",
|
|
40
|
+
"!dist/**/*.spec.*"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": [
|
|
43
|
+
"**/*.css"
|
|
44
|
+
],
|
|
45
|
+
"svelte": "./dist/index.js",
|
|
46
|
+
"types": "./dist/index.d.ts",
|
|
47
|
+
"type": "module",
|
|
48
|
+
"exports": {
|
|
49
|
+
".": {
|
|
50
|
+
"types": "./dist/index.d.ts",
|
|
51
|
+
"svelte": "./dist/index.js",
|
|
52
|
+
"default": "./dist/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./core": {
|
|
55
|
+
"types": "./dist/core/index.d.ts",
|
|
56
|
+
"svelte": "./dist/core/index.js",
|
|
57
|
+
"default": "./dist/core/index.js"
|
|
58
|
+
},
|
|
59
|
+
"./editor": {
|
|
60
|
+
"types": "./dist/editor/index.d.ts",
|
|
61
|
+
"svelte": "./dist/editor/index.js",
|
|
62
|
+
"default": "./dist/editor/index.js"
|
|
63
|
+
},
|
|
64
|
+
"./render": {
|
|
65
|
+
"types": "./dist/render/index.d.ts",
|
|
66
|
+
"svelte": "./dist/render/index.js",
|
|
67
|
+
"default": "./dist/render/index.js"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"svelte": "^5.0.0"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {
|
|
74
|
+
"@changesets/cli": "^2.31.0",
|
|
75
|
+
"@eslint/compat": "^2.0.2",
|
|
76
|
+
"@eslint/js": "^9.39.2",
|
|
77
|
+
"@playwright/test": "^1.58.2",
|
|
78
|
+
"@sveltejs/adapter-static": "^3.0.10",
|
|
79
|
+
"@sveltejs/kit": "^2.50.2",
|
|
80
|
+
"@sveltejs/package": "^2.5.7",
|
|
81
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.4",
|
|
82
|
+
"@tailwindcss/vite": "^4.2.4",
|
|
83
|
+
"@types/node": "^22",
|
|
84
|
+
"@vitest/browser-playwright": "^4.0.18",
|
|
85
|
+
"daisyui": "^5.5.19",
|
|
86
|
+
"eslint": "^9.39.2",
|
|
87
|
+
"eslint-config-prettier": "^10.1.8",
|
|
88
|
+
"eslint-plugin-svelte": "^3.14.0",
|
|
89
|
+
"globals": "^17.3.0",
|
|
90
|
+
"playwright": "^1.58.2",
|
|
91
|
+
"prettier": "^3.8.1",
|
|
92
|
+
"prettier-plugin-svelte": "^3.4.1",
|
|
93
|
+
"publint": "^0.3.17",
|
|
94
|
+
"svelte": "^5.51.0",
|
|
95
|
+
"svelte-check": "^4.4.2",
|
|
96
|
+
"tailwindcss": "^4.2.4",
|
|
97
|
+
"typescript": "^5.9.3",
|
|
98
|
+
"typescript-eslint": "^8.54.0",
|
|
99
|
+
"vite": "^7.3.1",
|
|
100
|
+
"vite-plugin-devtools-json": "^1.0.0",
|
|
101
|
+
"vitest": "^4.0.18",
|
|
102
|
+
"vitest-browser-svelte": "^2.0.2"
|
|
103
|
+
},
|
|
104
|
+
"keywords": [
|
|
105
|
+
"svelte"
|
|
106
|
+
],
|
|
107
|
+
"dependencies": {
|
|
108
|
+
"@tiptap/core": "^3.20.0",
|
|
109
|
+
"@tiptap/extension-code-block-lowlight": "3.20.0",
|
|
110
|
+
"@tiptap/html": "^3.20.0",
|
|
111
|
+
"@tiptap/pm": "^3.20.0",
|
|
112
|
+
"@tiptap/starter-kit": "^3.20.0",
|
|
113
|
+
"lowlight": "^3.3.0",
|
|
114
|
+
"phosphor-svelte": "^3.1.0"
|
|
115
|
+
}
|
|
116
|
+
}
|