uncial 0.0.7 → 2.0.0
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/README.md +82 -15
- package/dist/core/attributes.d.ts +21 -3
- package/dist/core/attributes.js +86 -15
- package/dist/core/defineBlock.js +26 -12
- package/dist/core/index.d.ts +3 -1
- package/dist/core/index.js +2 -1
- package/dist/core/issues.d.ts +7 -0
- package/dist/core/issues.js +9 -0
- package/dist/core/meta.js +2 -7
- package/dist/core/migrations.d.ts +30 -0
- package/dist/core/migrations.js +50 -0
- package/dist/core/normalize.d.ts +12 -1
- package/dist/core/normalize.js +123 -22
- package/dist/core/registry.d.ts +7 -0
- package/dist/core/registry.js +35 -4
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/types.d.ts +31 -3
- package/dist/core/validate.js +27 -7
- package/dist/editor/AttributeFieldControl.svelte +155 -7
- package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
- package/dist/editor/BlockAttributesPanel.svelte +76 -44
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -1
- package/dist/editor/Editor.svelte +149 -27
- package/dist/editor/Editor.svelte.d.ts +37 -0
- package/dist/editor/RichTextAttributeEditor.svelte +75 -145
- package/dist/editor/RichTextAttributeEditor.svelte.d.ts +2 -1
- package/dist/editor/Toolbar.svelte +2 -3
- package/dist/editor/attributeTargets.d.ts +23 -0
- package/dist/editor/attributeTargets.js +21 -0
- package/dist/editor/attributesController.d.ts +12 -0
- package/dist/editor/attributesController.js +142 -230
- package/dist/editor/bindEditor.d.ts +7 -0
- package/dist/editor/bindEditor.js +141 -27
- package/dist/editor/chooseAttribute.d.ts +26 -0
- package/dist/editor/chooseAttribute.js +10 -0
- package/dist/editor/containerChildOps.d.ts +33 -0
- package/dist/editor/containerChildOps.js +115 -0
- package/dist/editor/dropdownDismiss.d.ts +18 -0
- package/dist/editor/dropdownDismiss.js +46 -0
- package/dist/editor/dropdownFloat.d.ts +16 -0
- package/dist/editor/dropdownFloat.js +127 -0
- package/dist/editor/index.d.ts +3 -1
- package/dist/editor/index.js +2 -1
- package/dist/editor/linkController.d.ts +19 -0
- package/dist/editor/linkController.js +72 -0
- package/dist/editor/toolbarFeatures.d.ts +12 -0
- package/dist/editor/toolbarFeatures.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/render/Renderer.svelte +1 -1
- package/dist/render/RichContent.svelte +12 -4
- package/dist/render/RichContent.svelte.d.ts +2 -0
- package/dist/render/RichNode.svelte +80 -10
- package/dist/render/RichNode.svelte.d.ts +2 -0
- package/dist/render/Tab.svelte +27 -0
- package/dist/render/Tab.svelte.d.ts +10 -0
- package/dist/render/Tabs.svelte +57 -0
- package/dist/render/Tabs.svelte.d.ts +10 -0
- package/dist/render/index.d.ts +3 -0
- package/dist/render/index.js +6 -0
- package/dist/render/sanitize.d.ts +6 -0
- package/dist/render/sanitize.js +20 -1
- package/dist/render/tabSelection.svelte.d.ts +8 -0
- package/dist/render/tabSelection.svelte.js +40 -0
- package/dist/runtime/reactiveProps.svelte.d.ts +16 -0
- package/dist/runtime/reactiveProps.svelte.js +16 -0
- package/dist/runtime/svelte.d.ts +2 -1
- package/dist/runtime/svelte.js +26 -6
- package/dist/shared/EditorBlockFixture.svelte +24 -0
- package/dist/shared/EditorBlockFixture.svelte.d.ts +9 -0
- package/dist/shared/content.js +2 -1
- package/dist/shared/documentIdentity.d.ts +6 -0
- package/dist/shared/documentIdentity.js +64 -0
- package/dist/shared/guards.d.ts +13 -0
- package/dist/shared/guards.js +16 -0
- package/dist/shared/richText.js +20 -4
- package/dist/shared/runtimeId.d.ts +8 -0
- package/dist/shared/runtimeId.js +8 -0
- package/dist/shared/syntaxHighlight.d.ts +14 -0
- package/dist/shared/syntaxHighlight.js +36 -3
- package/dist/shared/tiptap.js +140 -27
- package/dist/styles/base.css +177 -0
- package/dist/styles/prose.css +26 -0
- package/dist/styles/tokens.css +53 -69
- package/dist/tabs.d.ts +6 -0
- package/dist/tabs.js +36 -0
- package/dist/web-components/UncialEditor.svelte +23 -7
- package/dist/web-components/UncialEditor.svelte.d.ts +7 -2
- package/dist/web-components/index.d.ts +45 -10
- package/dist/web-components/index.js +98 -36
- package/dist/web-components/reactiveProps.svelte.d.ts +10 -0
- package/dist/web-components/reactiveProps.svelte.js +13 -0
- package/package.json +134 -133
package/dist/runtime/svelte.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { createRawSnippet, mount, unmount } from 'svelte';
|
|
2
2
|
import { defineRuntimeBlock } from '../core/defineBlock.js';
|
|
3
3
|
import BlockNodeView from '../editor/BlockNodeView.svelte';
|
|
4
|
-
|
|
4
|
+
import { createReactiveProps } from './reactiveProps.svelte.js';
|
|
5
|
+
import { SVELTE_RUNTIME_ID } from '../shared/runtimeId.js';
|
|
6
|
+
// Re-exported so `uncial/runtime/svelte` consumers keep the same public surface.
|
|
7
|
+
export { SVELTE_RUNTIME_ID };
|
|
5
8
|
function isSvelteComponentDefinition(definition) {
|
|
6
9
|
return definition.runtime === SVELTE_RUNTIME_ID;
|
|
7
10
|
}
|
|
@@ -27,14 +30,20 @@ export const svelteRuntime = {
|
|
|
27
30
|
const host = document.createElement(options.inline ? 'span' : 'div');
|
|
28
31
|
host.className = 'uncial-nodeview-host';
|
|
29
32
|
options.target.replaceChildren(host);
|
|
30
|
-
const
|
|
33
|
+
const component = options.component.component;
|
|
34
|
+
// Prefer the first-class `contentDOM` option; fall back to the legacy
|
|
35
|
+
// `props.contentDOM` key for one release for any external caller still
|
|
36
|
+
// passing it that way.
|
|
37
|
+
const contentDOM = options.contentDOM ?? options.props.contentDOM ?? null;
|
|
38
|
+
const children = createContentSnippet(contentDOM);
|
|
39
|
+
const reactiveProps = createReactiveProps({
|
|
31
40
|
...options.props,
|
|
32
|
-
component
|
|
33
|
-
children
|
|
34
|
-
};
|
|
41
|
+
component,
|
|
42
|
+
children
|
|
43
|
+
});
|
|
35
44
|
let mounted = mount(BlockNodeView, {
|
|
36
45
|
target: host,
|
|
37
|
-
props: props
|
|
46
|
+
props: reactiveProps.props
|
|
38
47
|
});
|
|
39
48
|
return {
|
|
40
49
|
destroy() {
|
|
@@ -42,6 +51,17 @@ export const svelteRuntime = {
|
|
|
42
51
|
void unmount(mounted);
|
|
43
52
|
mounted = null;
|
|
44
53
|
}
|
|
54
|
+
},
|
|
55
|
+
update(nextProps) {
|
|
56
|
+
if (!mounted)
|
|
57
|
+
return;
|
|
58
|
+
// Keep the component identity and the content snippet stable so the
|
|
59
|
+
// mounted component (and its contentDOM) survives attribute updates.
|
|
60
|
+
reactiveProps.update({
|
|
61
|
+
...nextProps,
|
|
62
|
+
component,
|
|
63
|
+
children
|
|
64
|
+
});
|
|
45
65
|
}
|
|
46
66
|
};
|
|
47
67
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Test fixture: a custom block editor component with an attribute-bound input.
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
title?: string;
|
|
7
|
+
children?: Snippet;
|
|
8
|
+
updateAttributes?: (attrs: Record<string, unknown>) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let { title = '', children, updateAttributes }: Props = $props();
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<div data-testid="editor-block-fixture">
|
|
15
|
+
<input
|
|
16
|
+
data-testid="title-input"
|
|
17
|
+
value={title}
|
|
18
|
+
oninput={(event) => updateAttributes?.({ title: event.currentTarget.value })}
|
|
19
|
+
/>
|
|
20
|
+
<p data-testid="fixture-title">{title}</p>
|
|
21
|
+
{#if children}
|
|
22
|
+
<div data-testid="fixture-children">{@render children()}</div>
|
|
23
|
+
{/if}
|
|
24
|
+
</div>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
title?: string;
|
|
4
|
+
children?: Snippet;
|
|
5
|
+
updateAttributes?: (attrs: Record<string, unknown>) => void;
|
|
6
|
+
}
|
|
7
|
+
declare const EditorBlockFixture: import("svelte").Component<Props, {}, "">;
|
|
8
|
+
type EditorBlockFixture = ReturnType<typeof EditorBlockFixture>;
|
|
9
|
+
export default EditorBlockFixture;
|
package/dist/shared/content.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { normalizeMeta } from '../core/meta.js';
|
|
2
|
+
import { CURRENT_DOCUMENT_VERSION } from '../core/migrations.js';
|
|
2
3
|
export function emptyDocument(metaFields = new Map()) {
|
|
3
4
|
const document = {
|
|
4
5
|
type: 'doc',
|
|
5
|
-
version:
|
|
6
|
+
version: CURRENT_DOCUMENT_VERSION,
|
|
6
7
|
content: [{ type: 'paragraph' }]
|
|
7
8
|
};
|
|
8
9
|
if (metaFields.size > 0) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PMDoc, PMNode } from './document.js';
|
|
2
|
+
export declare function createBlockId(): string;
|
|
3
|
+
export declare function isBlockNode(node: Pick<PMNode, 'type'>): boolean;
|
|
4
|
+
export declare function headingSlug(node: PMNode): string;
|
|
5
|
+
export declare function stampNodeIdentity(node: PMNode, attrs: Record<string, unknown> | undefined, ids: Set<string>): Record<string, unknown> | undefined;
|
|
6
|
+
export declare function stampDocumentIdentity(document: PMDoc): PMDoc;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
let fallbackIdSequence = 0;
|
|
2
|
+
export function createBlockId() {
|
|
3
|
+
if (typeof globalThis.crypto?.randomUUID === 'function') {
|
|
4
|
+
return globalThis.crypto.randomUUID();
|
|
5
|
+
}
|
|
6
|
+
fallbackIdSequence += 1;
|
|
7
|
+
return `uncial-${Date.now().toString(36)}-${fallbackIdSequence.toString(36)}`;
|
|
8
|
+
}
|
|
9
|
+
export function isBlockNode(node) {
|
|
10
|
+
return node.type !== 'text' && node.type !== 'hardBreak';
|
|
11
|
+
}
|
|
12
|
+
function nodeText(node) {
|
|
13
|
+
if (node.type === 'text')
|
|
14
|
+
return node.text ?? '';
|
|
15
|
+
return node.content?.map(nodeText).join('') ?? '';
|
|
16
|
+
}
|
|
17
|
+
export function headingSlug(node) {
|
|
18
|
+
const slug = nodeText(node)
|
|
19
|
+
.normalize('NFKD')
|
|
20
|
+
.replace(/\p{M}/gu, '')
|
|
21
|
+
.toLowerCase()
|
|
22
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
23
|
+
.replace(/^-+|-+$/g, '');
|
|
24
|
+
return slug || 'section';
|
|
25
|
+
}
|
|
26
|
+
export function stampNodeIdentity(node, attrs, ids) {
|
|
27
|
+
if (!isBlockNode(node))
|
|
28
|
+
return attrs;
|
|
29
|
+
const persistedId = attrs?.id;
|
|
30
|
+
const id = typeof persistedId === 'string' && persistedId.length > 0 && !ids.has(persistedId)
|
|
31
|
+
? persistedId
|
|
32
|
+
: createBlockId();
|
|
33
|
+
ids.add(id);
|
|
34
|
+
const stamped = { ...attrs, id };
|
|
35
|
+
if (node.type === 'heading' && typeof attrs?.slug !== 'string') {
|
|
36
|
+
stamped.slug = headingSlug(node);
|
|
37
|
+
}
|
|
38
|
+
return stamped;
|
|
39
|
+
}
|
|
40
|
+
function stampNode(node, ids) {
|
|
41
|
+
const content = node.content?.map((child) => stampNode(child, ids));
|
|
42
|
+
const stamped = { ...node };
|
|
43
|
+
if (content === undefined) {
|
|
44
|
+
delete stamped.content;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
stamped.content = content;
|
|
48
|
+
}
|
|
49
|
+
const attrs = stampNodeIdentity(stamped, node.attrs, ids);
|
|
50
|
+
if (attrs === undefined) {
|
|
51
|
+
delete stamped.attrs;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
stamped.attrs = attrs;
|
|
55
|
+
}
|
|
56
|
+
return stamped;
|
|
57
|
+
}
|
|
58
|
+
export function stampDocumentIdentity(document) {
|
|
59
|
+
const ids = new Set();
|
|
60
|
+
return {
|
|
61
|
+
...document,
|
|
62
|
+
content: document.content?.map((node) => stampNode(node, ids))
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AttributeOption } from '../core/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Canonical "is this a plain (non-array) object?" guard. Several core/shared
|
|
4
|
+
* modules previously each defined their own copy (`isPlainObject` / `isRecord`);
|
|
5
|
+
* they were byte-identical, so they share this one implementation.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Canonical guard distinguishing an `AttributeOption<T>` object (`{ value, ... }`)
|
|
10
|
+
* from a bare option value. Previously duplicated in `core/attributes.ts` and
|
|
11
|
+
* `core/defineBlock.ts`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isAttributeOption<T>(value: unknown): value is AttributeOption<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical "is this a plain (non-array) object?" guard. Several core/shared
|
|
3
|
+
* modules previously each defined their own copy (`isPlainObject` / `isRecord`);
|
|
4
|
+
* they were byte-identical, so they share this one implementation.
|
|
5
|
+
*/
|
|
6
|
+
export function isPlainObject(value) {
|
|
7
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Canonical guard distinguishing an `AttributeOption<T>` object (`{ value, ... }`)
|
|
11
|
+
* from a bare option value. Previously duplicated in `core/attributes.ts` and
|
|
12
|
+
* `core/defineBlock.ts`.
|
|
13
|
+
*/
|
|
14
|
+
export function isAttributeOption(value) {
|
|
15
|
+
return isPlainObject(value) && 'value' in value;
|
|
16
|
+
}
|
package/dist/shared/richText.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isPlainObject as isRecord } from './guards.js';
|
|
1
2
|
export const supportedRichTextFeatures = [
|
|
2
3
|
'bold',
|
|
3
4
|
'italic',
|
|
@@ -21,9 +22,10 @@ const defaultRichTextFeatures = [
|
|
|
21
22
|
'hardBreak'
|
|
22
23
|
];
|
|
23
24
|
const supportedFeatureSet = new Set(supportedRichTextFeatures);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
// `resolveRichTextFeatures` runs inside reactive `$derived`s, so an unsupported
|
|
26
|
+
// feature would otherwise warn on every re-render. Warn once per process per
|
|
27
|
+
// feature instead.
|
|
28
|
+
const warnedUnsupportedFeatures = new Set();
|
|
27
29
|
function isPMNode(value) {
|
|
28
30
|
if (!isRecord(value) || typeof value.type !== 'string')
|
|
29
31
|
return false;
|
|
@@ -75,13 +77,27 @@ function hasNodeText(nodes = []) {
|
|
|
75
77
|
});
|
|
76
78
|
}
|
|
77
79
|
export function hasRichTextContent(value) {
|
|
78
|
-
|
|
80
|
+
// Sound narrowing: only report `true` (and narrow to `PMDoc`) when the value
|
|
81
|
+
// really is a rich-text document that contains visible text. A bare string
|
|
82
|
+
// has visible text but is NOT a `PMDoc`, so narrowing it to `PMDoc` was
|
|
83
|
+
// unsound; such inputs now return `false`.
|
|
84
|
+
return isRichTextDocument(value) && hasNodeText(value.content ?? []);
|
|
79
85
|
}
|
|
80
86
|
export function resolveRichTextFeatures(features) {
|
|
81
87
|
if (features === '*' || features === '__all__')
|
|
82
88
|
return new Set(runtimeRichTextFeatures);
|
|
83
89
|
if (!Array.isArray(features))
|
|
84
90
|
return new Set(defaultRichTextFeatures);
|
|
91
|
+
// `link` is not a rich-text attribute feature: the rich-text runtime
|
|
92
|
+
// (`createRichTextExtensions`) intentionally ships no link mark or link UI.
|
|
93
|
+
// Rather than dropping an explicitly-requested `'link'` silently, warn so the
|
|
94
|
+
// misconfiguration is visible instead of a link that never appears.
|
|
95
|
+
if (features.includes('link') && !warnedUnsupportedFeatures.has('link')) {
|
|
96
|
+
warnedUnsupportedFeatures.add('link');
|
|
97
|
+
console.warn("resolveRichTextFeatures: the 'link' feature is not supported in rich-text " +
|
|
98
|
+
'attribute editors and was ignored. Remove it from the feature list, or use a ' +
|
|
99
|
+
'full block/link mark for linkable content.');
|
|
100
|
+
}
|
|
85
101
|
return new Set(features.filter((feature) => typeof feature === 'string' && supportedFeatureSet.has(feature) && feature !== 'link'));
|
|
86
102
|
}
|
|
87
103
|
export function filterRichTextNodes(nodes = [], features) {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier for the built-in Svelte block runtime.
|
|
3
|
+
*
|
|
4
|
+
* This lives in a dependency-free leaf module so SSR/render consumers can compare
|
|
5
|
+
* against it without importing `runtime/svelte.ts`, which pulls in the editor mount
|
|
6
|
+
* stack (Tiptap `BlockNodeView`, Svelte `mount`/`unmount`, reactive props).
|
|
7
|
+
*/
|
|
8
|
+
export declare const SVELTE_RUNTIME_ID = "svelte";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier for the built-in Svelte block runtime.
|
|
3
|
+
*
|
|
4
|
+
* This lives in a dependency-free leaf module so SSR/render consumers can compare
|
|
5
|
+
* against it without importing `runtime/svelte.ts`, which pulls in the editor mount
|
|
6
|
+
* stack (Tiptap `BlockNodeView`, Svelte `mount`/`unmount`, reactive props).
|
|
7
|
+
*/
|
|
8
|
+
export const SVELTE_RUNTIME_ID = 'svelte';
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registered grammars, named one by one rather than lowlight's `common` set.
|
|
3
|
+
*
|
|
4
|
+
* `common` is 37 grammars and about 172 KB minified, and it lands in the chunk
|
|
5
|
+
* every page rendering a document hydrates — including pages that contain no
|
|
6
|
+
* code at all. These are the languages the alias table below maps, which is the
|
|
7
|
+
* set this renderer can actually name.
|
|
8
|
+
*
|
|
9
|
+
* `plaintext` is registered for the opposite reason to the rest: it matches
|
|
10
|
+
* nothing, and that is its job. An unregistered language falls to
|
|
11
|
+
* `highlightAuto`, so a block declared `plaintext` — console output, a directory
|
|
12
|
+
* tree, a licence key — would be guessed at and coloured, which is precisely
|
|
13
|
+
* what declaring it asks the renderer not to do.
|
|
14
|
+
*/
|
|
1
15
|
export declare const lowlight: {
|
|
2
16
|
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options> | null | undefined) => import("hast").Root;
|
|
3
17
|
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions> | null | undefined) => import("hast").Root;
|
|
@@ -1,10 +1,43 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import bash from 'highlight.js/lib/languages/bash';
|
|
2
|
+
import css from 'highlight.js/lib/languages/css';
|
|
3
|
+
import javascript from 'highlight.js/lib/languages/javascript';
|
|
4
|
+
import json from 'highlight.js/lib/languages/json';
|
|
5
|
+
import plaintext from 'highlight.js/lib/languages/plaintext';
|
|
6
|
+
import python from 'highlight.js/lib/languages/python';
|
|
7
|
+
import typescript from 'highlight.js/lib/languages/typescript';
|
|
8
|
+
import xml from 'highlight.js/lib/languages/xml';
|
|
9
|
+
import { createLowlight } from 'lowlight';
|
|
10
|
+
/**
|
|
11
|
+
* Registered grammars, named one by one rather than lowlight's `common` set.
|
|
12
|
+
*
|
|
13
|
+
* `common` is 37 grammars and about 172 KB minified, and it lands in the chunk
|
|
14
|
+
* every page rendering a document hydrates — including pages that contain no
|
|
15
|
+
* code at all. These are the languages the alias table below maps, which is the
|
|
16
|
+
* set this renderer can actually name.
|
|
17
|
+
*
|
|
18
|
+
* `plaintext` is registered for the opposite reason to the rest: it matches
|
|
19
|
+
* nothing, and that is its job. An unregistered language falls to
|
|
20
|
+
* `highlightAuto`, so a block declared `plaintext` — console output, a directory
|
|
21
|
+
* tree, a licence key — would be guessed at and coloured, which is precisely
|
|
22
|
+
* what declaring it asks the renderer not to do.
|
|
23
|
+
*/
|
|
24
|
+
export const lowlight = createLowlight({
|
|
25
|
+
bash,
|
|
26
|
+
css,
|
|
27
|
+
javascript,
|
|
28
|
+
json,
|
|
29
|
+
plaintext,
|
|
30
|
+
python,
|
|
31
|
+
typescript,
|
|
32
|
+
xml
|
|
33
|
+
});
|
|
3
34
|
lowlight.registerAlias({
|
|
4
35
|
javascript: ['js', 'jsx'],
|
|
5
36
|
python: ['py'],
|
|
6
37
|
typescript: ['ts', 'tsx'],
|
|
7
|
-
|
|
38
|
+
// Vue single-file components are XML-shaped, and naming the alias keeps them
|
|
39
|
+
// off the auto-detect path, whose guess narrowed with the registry above.
|
|
40
|
+
xml: ['html', 'svelte', 'vue']
|
|
8
41
|
});
|
|
9
42
|
function escapeHtml(value) {
|
|
10
43
|
return value
|
package/dist/shared/tiptap.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import StarterKit from '@tiptap/starter-kit';
|
|
2
|
+
import Code from '@tiptap/extension-code';
|
|
2
3
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { Table, TableCell, TableHeader, TableRow } from '@tiptap/extension-table';
|
|
5
|
+
import { Extension, Mark, mergeAttributes, Node } from '@tiptap/core';
|
|
6
|
+
import { DEFAULT_MARKS, resolveRegistry } from '../core/registry.js';
|
|
5
7
|
import { coerceAttributeValue, normalizeBlockAttributes, serializeBlockAttributes } from '../core/attributes.js';
|
|
6
8
|
import { lowlight } from './syntaxHighlight.js';
|
|
7
9
|
import { sanitizeHref } from '../render/sanitize.js';
|
|
@@ -19,6 +21,31 @@ function safeParseJSON(raw) {
|
|
|
19
21
|
export function getBlockDefaultAttrs(block) {
|
|
20
22
|
return normalizeBlockAttributes(block, {});
|
|
21
23
|
}
|
|
24
|
+
function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
|
|
25
|
+
return {
|
|
26
|
+
attrs: node.attrs ?? {},
|
|
27
|
+
content: (node.content?.toJSON() ?? []),
|
|
28
|
+
blockId: block.id,
|
|
29
|
+
label: block.label,
|
|
30
|
+
draggable: block.behaviors.draggable ?? true,
|
|
31
|
+
// Deliberately no `.focus()` here: updates often originate from form
|
|
32
|
+
// controls inside the block component, and refocusing the editor view
|
|
33
|
+
// would steal focus from them on every keystroke.
|
|
34
|
+
updateAttributes: block.readOnly
|
|
35
|
+
? undefined
|
|
36
|
+
: (attrs) => editor?.chain().updateAttributes(block.id, attrs).run(),
|
|
37
|
+
// ProseMirror's getPos can return undefined when the node is not currently
|
|
38
|
+
// in the document (e.g. mid-transaction); skip activation in that case
|
|
39
|
+
// rather than reporting a bogus position.
|
|
40
|
+
onActivate: getPos && onActivate
|
|
41
|
+
? () => {
|
|
42
|
+
const pos = getPos();
|
|
43
|
+
if (pos !== undefined)
|
|
44
|
+
onActivate(pos);
|
|
45
|
+
}
|
|
46
|
+
: undefined
|
|
47
|
+
};
|
|
48
|
+
}
|
|
22
49
|
function mountBlockEditorComponent(block, node, container, contentDOM, mounted, editor, onActivate, getPos) {
|
|
23
50
|
mounted?.destroy();
|
|
24
51
|
const createEditorMount = block.components.editor.plugin.createEditorMount;
|
|
@@ -29,16 +56,8 @@ function mountBlockEditorComponent(block, node, container, contentDOM, mounted,
|
|
|
29
56
|
target: container,
|
|
30
57
|
inline: block.behaviors.inline ?? false,
|
|
31
58
|
component: block.components.editor,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
content: (node.content?.toJSON() ?? []),
|
|
35
|
-
contentDOM,
|
|
36
|
-
blockId: block.id,
|
|
37
|
-
label: block.label,
|
|
38
|
-
draggable: block.behaviors.draggable ?? true,
|
|
39
|
-
updateAttributes: (attrs) => editor?.chain().focus().updateAttributes(block.id, attrs).run(),
|
|
40
|
-
onActivate: getPos && onActivate ? () => onActivate(getPos()) : undefined
|
|
41
|
-
}
|
|
59
|
+
contentDOM,
|
|
60
|
+
props: buildBlockEditorProps(block, node, editor, onActivate, getPos)
|
|
42
61
|
});
|
|
43
62
|
}
|
|
44
63
|
function isDragHandleEvent(event) {
|
|
@@ -64,6 +83,8 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
|
64
83
|
if (!getPos)
|
|
65
84
|
return;
|
|
66
85
|
const pos = getPos();
|
|
86
|
+
if (pos === undefined)
|
|
87
|
+
return;
|
|
67
88
|
dom.dataset.uncialBlockPos = String(pos);
|
|
68
89
|
}
|
|
69
90
|
if (contentDOM) {
|
|
@@ -85,7 +106,23 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
|
85
106
|
return true;
|
|
86
107
|
}
|
|
87
108
|
lastAttrs = nextAttrs;
|
|
88
|
-
|
|
109
|
+
if (mounted?.update) {
|
|
110
|
+
// Update the mounted component's props in place so DOM state (focus,
|
|
111
|
+
// selection, scroll position) inside the block component survives.
|
|
112
|
+
//
|
|
113
|
+
// Queued, not applied here: this runs inside `view.dispatch`, itself
|
|
114
|
+
// inside the event handler that edited the attribute, and a prop
|
|
115
|
+
// written to a separately mounted component in that window does not
|
|
116
|
+
// reach it — the block would keep rendering its old attributes until
|
|
117
|
+
// something else re-rendered it. A microtask is outside that window
|
|
118
|
+
// and still lands before paint.
|
|
119
|
+
const nextProps = buildBlockEditorProps(block, nextNode, editor, onActivate, getPos);
|
|
120
|
+
queueMicrotask(() => mounted?.update?.(nextProps));
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Fall back to a full remount for runtimes without update support.
|
|
124
|
+
mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, editor, onActivate, getPos);
|
|
125
|
+
}
|
|
89
126
|
return true;
|
|
90
127
|
},
|
|
91
128
|
stopEvent(event) {
|
|
@@ -109,13 +146,33 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
|
109
146
|
}
|
|
110
147
|
};
|
|
111
148
|
}
|
|
112
|
-
function
|
|
149
|
+
function containerContentExpression(content) {
|
|
150
|
+
// Core "flow" semantics (see core/normalize.ts) preserve any block-level
|
|
151
|
+
// children inside a container: paragraphs, headings, lists, code blocks,
|
|
152
|
+
// blockquotes, and custom blocks alike. Every non-inline node in the editor
|
|
153
|
+
// schema (StarterKit nodes and custom blocks) belongs to the "block" group,
|
|
154
|
+
// so "block*" mirrors what core considers valid flow content. Zero-or-more
|
|
155
|
+
// (not one-or-more) is deliberate: persisted documents commonly contain
|
|
156
|
+
// empty containers (`"content": []`), and those must stay schema-valid.
|
|
157
|
+
// A container that names its admissible children gets those names in place
|
|
158
|
+
// of the whole "block" group, which is what makes the constraint hold for
|
|
159
|
+
// paste and for any other route a child can take into the document.
|
|
160
|
+
if (content.allowedBlocks?.length) {
|
|
161
|
+
return `(${content.allowedBlocks.join(' | ')})*`;
|
|
162
|
+
}
|
|
163
|
+
switch (content.kind) {
|
|
164
|
+
case 'flow':
|
|
165
|
+
default:
|
|
166
|
+
return 'block*';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function createBlockNodeExtension(block, onActivate) {
|
|
113
170
|
const isContainer = Boolean(block.content);
|
|
114
171
|
return Node.create({
|
|
115
172
|
name: block.id,
|
|
116
173
|
group: block.behaviors.inline ? 'inline' : 'block',
|
|
117
174
|
inline: block.behaviors.inline ?? false,
|
|
118
|
-
content:
|
|
175
|
+
content: block.content ? containerContentExpression(block.content) : undefined,
|
|
119
176
|
atom: !isContainer,
|
|
120
177
|
defining: isContainer,
|
|
121
178
|
isolating: isContainer,
|
|
@@ -175,7 +232,9 @@ const LinkMark = Mark.create({
|
|
|
175
232
|
parseHTML: (element) => sanitizeHref(element.getAttribute('href'))
|
|
176
233
|
},
|
|
177
234
|
target: { default: null },
|
|
178
|
-
rel: { default: null }
|
|
235
|
+
rel: { default: null },
|
|
236
|
+
title: { default: null },
|
|
237
|
+
class: { default: null }
|
|
179
238
|
};
|
|
180
239
|
},
|
|
181
240
|
parseHTML() {
|
|
@@ -195,8 +254,57 @@ const LinkMark = Mark.create({
|
|
|
195
254
|
};
|
|
196
255
|
}
|
|
197
256
|
});
|
|
257
|
+
function createDocumentIdentityExtension(registry) {
|
|
258
|
+
const blockTypes = [
|
|
259
|
+
'paragraph',
|
|
260
|
+
'heading',
|
|
261
|
+
'blockquote',
|
|
262
|
+
'codeBlock',
|
|
263
|
+
'horizontalRule',
|
|
264
|
+
'bulletList',
|
|
265
|
+
'orderedList',
|
|
266
|
+
'listItem',
|
|
267
|
+
'table',
|
|
268
|
+
'tableRow',
|
|
269
|
+
'tableCell',
|
|
270
|
+
'tableHeader',
|
|
271
|
+
...registry.blocks.filter((block) => !block.behaviors.inline).map((block) => block.id)
|
|
272
|
+
];
|
|
273
|
+
return Extension.create({
|
|
274
|
+
name: 'documentIdentity',
|
|
275
|
+
addGlobalAttributes() {
|
|
276
|
+
return [
|
|
277
|
+
{
|
|
278
|
+
types: blockTypes,
|
|
279
|
+
attributes: {
|
|
280
|
+
id: { default: null, rendered: false }
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
types: ['heading'],
|
|
285
|
+
attributes: {
|
|
286
|
+
slug: { default: null, rendered: false }
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Inline code that may carry other marks.
|
|
295
|
+
*
|
|
296
|
+
* Tiptap's Code ships `excludes: "_"`, the wildcard, so code cannot combine
|
|
297
|
+
* with emphasis or a link. ProseMirror does not report that as an error: it
|
|
298
|
+
* drops the containing node while parsing, so a document holding bold code
|
|
299
|
+
* loses whole blocks on the round trip through the editor, silently.
|
|
300
|
+
*
|
|
301
|
+
* The renderer nests marks, and documents are authored against the renderer —
|
|
302
|
+
* bold code is what "**`media-src`**" means in prose — so the editor's schema
|
|
303
|
+
* is the side that has to give.
|
|
304
|
+
*/
|
|
305
|
+
const CombinableCode = Code.extend({ excludes: '' });
|
|
198
306
|
function createBaseExtensions(schema, extensions = []) {
|
|
199
|
-
const marks = schema?.allowedMarks ?? new Set(
|
|
307
|
+
const marks = schema?.allowedMarks ?? new Set(DEFAULT_MARKS);
|
|
200
308
|
const includeBold = marks.has('bold');
|
|
201
309
|
const includeItalic = marks.has('italic');
|
|
202
310
|
const includeStrike = marks.has('strike');
|
|
@@ -207,7 +315,7 @@ function createBaseExtensions(schema, extensions = []) {
|
|
|
207
315
|
bold: includeBold ? {} : false,
|
|
208
316
|
italic: includeItalic ? {} : false,
|
|
209
317
|
strike: includeStrike ? {} : false,
|
|
210
|
-
code:
|
|
318
|
+
code: false,
|
|
211
319
|
heading: { levels: [2, 3, 4, 5, 6] },
|
|
212
320
|
bulletList: {},
|
|
213
321
|
orderedList: {},
|
|
@@ -218,6 +326,11 @@ function createBaseExtensions(schema, extensions = []) {
|
|
|
218
326
|
link: false
|
|
219
327
|
}),
|
|
220
328
|
CodeBlockLowlight.configure({ lowlight }),
|
|
329
|
+
Table.configure({ resizable: false }),
|
|
330
|
+
TableRow,
|
|
331
|
+
TableHeader,
|
|
332
|
+
TableCell,
|
|
333
|
+
...(includeCode ? [CombinableCode] : []),
|
|
221
334
|
...(includeLink ? [LinkMark] : []),
|
|
222
335
|
...extensions
|
|
223
336
|
];
|
|
@@ -228,7 +341,7 @@ export function createRichTextExtensions(features) {
|
|
|
228
341
|
bold: features.has('bold') ? {} : false,
|
|
229
342
|
italic: features.has('italic') ? {} : false,
|
|
230
343
|
strike: features.has('strike') ? {} : false,
|
|
231
|
-
code:
|
|
344
|
+
code: false,
|
|
232
345
|
heading: features.has('heading') ? {} : false,
|
|
233
346
|
bulletList: features.has('bulletList') ? {} : false,
|
|
234
347
|
orderedList: features.has('orderedList') ? {} : false,
|
|
@@ -238,19 +351,19 @@ export function createRichTextExtensions(features) {
|
|
|
238
351
|
hardBreak: features.has('hardBreak') ? {} : false,
|
|
239
352
|
link: false
|
|
240
353
|
}),
|
|
354
|
+
...(features.has('code') ? [CombinableCode] : []),
|
|
241
355
|
...(features.has('codeBlock') ? [CodeBlockLowlight.configure({ lowlight })] : [])
|
|
242
356
|
];
|
|
243
357
|
}
|
|
244
358
|
export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = []) {
|
|
245
359
|
const registry = resolveRegistry(blocks);
|
|
246
|
-
const allowedBlockNames = registry.blocks
|
|
247
|
-
.filter((block) => !schema || schema.allowedBlocks.has(block.id))
|
|
248
|
-
.map((block) => block.id);
|
|
249
|
-
const containerContentExpression = allowedBlockNames.length
|
|
250
|
-
? `(${allowedBlockNames.join(' | ')})*`
|
|
251
|
-
: '';
|
|
252
360
|
const blockExtensions = registry.blocks
|
|
253
361
|
.filter((block) => !schema || schema.allowedBlocks.has(block.id))
|
|
254
|
-
.map((block) => createBlockNodeExtension(block,
|
|
255
|
-
return [
|
|
362
|
+
.map((block) => createBlockNodeExtension(block, onActivateBlock));
|
|
363
|
+
return [
|
|
364
|
+
...createBaseExtensions(schema),
|
|
365
|
+
createDocumentIdentityExtension(registry),
|
|
366
|
+
...blockExtensions,
|
|
367
|
+
...extensions
|
|
368
|
+
];
|
|
256
369
|
}
|