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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Flood
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# Uncial
|
|
2
|
+
|
|
3
|
+
Uncial is a backend-agnostic Svelte block editor built on Tiptap. Library consumers define a block once as a Svelte component, register it with `defineBlock(...)`, and reuse that same block definition in both the WYSIWYG editor and the renderer.
|
|
4
|
+
|
|
5
|
+
Custom blocks can stay atomic or declare one default child content region for nested document flow.
|
|
6
|
+
|
|
7
|
+
## What ships today
|
|
8
|
+
|
|
9
|
+
- Shared block definitions for editor and renderer
|
|
10
|
+
- Atomic blocks and container blocks with nested child content
|
|
11
|
+
- Registry and schema helpers for block and mark allowlists
|
|
12
|
+
- Typed attribute normalization for strings, numbers, booleans, and JSON-like fields
|
|
13
|
+
- Document normalization with version stamping
|
|
14
|
+
- Validation hooks for editor and renderer boundaries
|
|
15
|
+
- SSR-safe renderer imports separated from browser-only editor behavior
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
bun add uncial
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Peer dependency:
|
|
24
|
+
|
|
25
|
+
- `svelte@^5`
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```svelte
|
|
30
|
+
<script lang="ts">
|
|
31
|
+
import {
|
|
32
|
+
Editor,
|
|
33
|
+
Renderer,
|
|
34
|
+
createBlockAttributesController,
|
|
35
|
+
createBlockRegistry,
|
|
36
|
+
createSchema,
|
|
37
|
+
defineBlock
|
|
38
|
+
} from 'uncial';
|
|
39
|
+
import PromoCard from './PromoCard.svelte';
|
|
40
|
+
|
|
41
|
+
const promoCard = defineBlock({
|
|
42
|
+
id: 'promoCard',
|
|
43
|
+
label: 'Promo Card',
|
|
44
|
+
attributes: {
|
|
45
|
+
title: '',
|
|
46
|
+
featured: false,
|
|
47
|
+
priority: 0,
|
|
48
|
+
metadata: { default: { theme: 'sand' }, input: 'json' }
|
|
49
|
+
},
|
|
50
|
+
component: PromoCard
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const blocks = createBlockRegistry([promoCard]);
|
|
54
|
+
const schema = createSchema(blocks);
|
|
55
|
+
const attributesController = createBlockAttributesController();
|
|
56
|
+
|
|
57
|
+
let document = {
|
|
58
|
+
type: 'doc',
|
|
59
|
+
content: [{ type: 'paragraph' }]
|
|
60
|
+
};
|
|
61
|
+
</script>
|
|
62
|
+
|
|
63
|
+
<Editor {blocks} {schema} {attributesController} bind:json={document} />
|
|
64
|
+
<Renderer content={document} {blocks} {schema} />
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Styling and customization
|
|
68
|
+
|
|
69
|
+
The root `Editor` and `Renderer` exports ship with Uncial's starter shell and default component-scoped styling.
|
|
70
|
+
|
|
71
|
+
If you want to own the editor layout yourself, use `bindEditor(...)` on an element you control:
|
|
72
|
+
|
|
73
|
+
```svelte
|
|
74
|
+
<script lang="ts">
|
|
75
|
+
import type { Editor as TiptapEditor } from '@tiptap/core';
|
|
76
|
+
import { bindEditor, Renderer } from 'uncial';
|
|
77
|
+
|
|
78
|
+
let document = $state({
|
|
79
|
+
type: 'doc',
|
|
80
|
+
content: [{ type: 'paragraph' }]
|
|
81
|
+
});
|
|
82
|
+
let editor = $state<TiptapEditor | null>(null);
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<div
|
|
86
|
+
use:bindEditor={{
|
|
87
|
+
blocks,
|
|
88
|
+
schema,
|
|
89
|
+
json: document,
|
|
90
|
+
onChange: (nextDocument) => {
|
|
91
|
+
document = nextDocument;
|
|
92
|
+
},
|
|
93
|
+
onEditor: (nextEditor) => {
|
|
94
|
+
editor = nextEditor;
|
|
95
|
+
}
|
|
96
|
+
}}
|
|
97
|
+
/>
|
|
98
|
+
<Renderer content={document} {blocks} {schema} />
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Block definition
|
|
102
|
+
|
|
103
|
+
Use `component` when the same Svelte component should render in both the editor and the frontend output.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const hero = defineBlock({
|
|
107
|
+
id: 'hero',
|
|
108
|
+
label: 'Hero',
|
|
109
|
+
attributes: {
|
|
110
|
+
title: '',
|
|
111
|
+
subtitle: { default: '', input: 'textarea' },
|
|
112
|
+
featured: false,
|
|
113
|
+
priority: 1,
|
|
114
|
+
settings: { default: { align: 'left' }, input: 'json' }
|
|
115
|
+
},
|
|
116
|
+
component: Hero
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Attribute specs support:
|
|
121
|
+
|
|
122
|
+
- `default`: required default value
|
|
123
|
+
- `required`: require a value at validation time
|
|
124
|
+
- `validate`: custom validation predicate
|
|
125
|
+
- `parse`: custom coercion from editor or serialized input
|
|
126
|
+
- `serialize`: custom serialization for HTML persistence
|
|
127
|
+
- `input`: one of `text`, `textarea`, `number`, `checkbox`, or `json`
|
|
128
|
+
- `placeholder`: optional editor placeholder
|
|
129
|
+
|
|
130
|
+
Blocks can also opt into child content:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const collapsible = defineBlock({
|
|
134
|
+
id: 'collapsible',
|
|
135
|
+
label: 'Collapsible',
|
|
136
|
+
attributes: {
|
|
137
|
+
title: ''
|
|
138
|
+
},
|
|
139
|
+
component: Collapsible,
|
|
140
|
+
content: { kind: 'flow' }
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Container block components receive:
|
|
145
|
+
|
|
146
|
+
- attribute props as usual
|
|
147
|
+
- `content`: normalized child `PMNode[]`
|
|
148
|
+
- `children`: a built-in snippet for rendering or placing the child region
|
|
149
|
+
|
|
150
|
+
Example:
|
|
151
|
+
|
|
152
|
+
```svelte
|
|
153
|
+
<script lang="ts">
|
|
154
|
+
import type { Snippet } from 'svelte';
|
|
155
|
+
|
|
156
|
+
interface Props {
|
|
157
|
+
title?: string;
|
|
158
|
+
children?: Snippet;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let { title = '', children }: Props = $props();
|
|
162
|
+
</script>
|
|
163
|
+
|
|
164
|
+
<details>
|
|
165
|
+
<summary>{title}</summary>
|
|
166
|
+
<div>
|
|
167
|
+
{#if children}
|
|
168
|
+
{@render children()}
|
|
169
|
+
{/if}
|
|
170
|
+
</div>
|
|
171
|
+
</details>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Content model
|
|
175
|
+
|
|
176
|
+
- Documents are ProseMirror-compatible JSON objects
|
|
177
|
+
- `normalizeDocument(...)` stamps the current document version and coerces known block attributes
|
|
178
|
+
- Unknown custom block attributes are stripped during normalization
|
|
179
|
+
- Atomic custom blocks drop accidental child content during normalization
|
|
180
|
+
- Container custom blocks preserve validated child content
|
|
181
|
+
- Disallowed marks are removed when a schema is supplied
|
|
182
|
+
|
|
183
|
+
## Validation
|
|
184
|
+
|
|
185
|
+
Use `validateDocument(...)` directly or pass `onIssue` into `Editor` or `Renderer` to observe issues during normalization and render flows.
|
|
186
|
+
|
|
187
|
+
```svelte
|
|
188
|
+
<Editor
|
|
189
|
+
{blocks}
|
|
190
|
+
{schema}
|
|
191
|
+
bind:json={document}
|
|
192
|
+
onIssue={(issue) => console.warn(issue.code, issue.path)}
|
|
193
|
+
/
|
|
194
|
+
>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Rendering and security
|
|
198
|
+
|
|
199
|
+
- Renderer output is driven by the same block registry used by the editor
|
|
200
|
+
- Built-in rich text rendering supports headings, lists, blockquotes, code blocks, inline code, strike, bold, italic, and links
|
|
201
|
+
- Links are sanitized to allow only `http`, `https`, `mailto`, `tel`, relative paths, and hash links
|
|
202
|
+
|
|
203
|
+
## Development
|
|
204
|
+
|
|
205
|
+
```sh
|
|
206
|
+
bun run check
|
|
207
|
+
bun run test:unit -- --run
|
|
208
|
+
bun run build
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Additional suites:
|
|
212
|
+
|
|
213
|
+
- `bun run test:browser -- --run` for browser-backed Svelte component tests
|
|
214
|
+
- `bun run test:e2e` for Playwright end-to-end tests
|
|
215
|
+
|
|
216
|
+
Browser-backed tests require Playwright browsers to be installed:
|
|
217
|
+
|
|
218
|
+
```sh
|
|
219
|
+
bunx playwright install
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Releasing
|
|
223
|
+
|
|
224
|
+
For user-facing library changes, run `bun run changeset` and commit the generated changeset with your PR. After regular PRs merge into `main`, Changesets opens or updates a version PR. Merging that version PR publishes `uncial` to npm with trusted publishing/provenance and creates the GitHub release.
|
|
225
|
+
|
|
226
|
+
## Status
|
|
227
|
+
|
|
228
|
+
Uncial is currently a production-hardening library project rather than a finished CMS platform. The API is focused on typed custom blocks with editor/render parity, including container-style blocks with nested content.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AttributeInputKind, AttributeOption, AttributeSpec, BlockDefinition } from './types.js';
|
|
2
|
+
export type AttributeDefinition = Pick<BlockDefinition, 'attributes'>;
|
|
3
|
+
export declare function normalizeAttributeOptions<T>(spec: AttributeSpec<T>): AttributeOption<T>[] | undefined;
|
|
4
|
+
export declare function inferAttributeInputKind(spec: AttributeSpec<unknown>): AttributeInputKind;
|
|
5
|
+
export declare function coerceAttributeValue<T>(spec: AttributeSpec<T>, value: unknown): T;
|
|
6
|
+
export declare function normalizeBlockAttributes(block: AttributeDefinition, attrs?: Record<string, unknown>): Record<string, unknown>;
|
|
7
|
+
export declare function serializeAttributeValue<T>(spec: AttributeSpec<T>, value: T): unknown;
|
|
8
|
+
export declare function serializeBlockAttributes(block: AttributeDefinition, attrs: Record<string, unknown>): string;
|
|
9
|
+
export declare function toAttributeDraftValue(spec: AttributeSpec<unknown>, value: unknown): unknown;
|
|
10
|
+
export declare function toAttributeDraftValues(block: AttributeDefinition, attrs: Record<string, unknown>): Record<string, unknown>;
|
|
11
|
+
export declare function parseBlockDraftAttributes(block: AttributeDefinition, draftAttrs: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { coerceRichTextDocument } from '../shared/richText.js';
|
|
2
|
+
function isPlainObject(value) {
|
|
3
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function parseJsonValue(raw) {
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(raw);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function isAttributeOption(value) {
|
|
14
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && 'value' in value;
|
|
15
|
+
}
|
|
16
|
+
export function normalizeAttributeOptions(spec) {
|
|
17
|
+
if (!spec.options || spec.options.length === 0)
|
|
18
|
+
return undefined;
|
|
19
|
+
return spec.options.map((option) => {
|
|
20
|
+
if (isAttributeOption(option)) {
|
|
21
|
+
return {
|
|
22
|
+
value: option.value,
|
|
23
|
+
label: option.label ?? String(option.value),
|
|
24
|
+
description: option.description
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
value: option,
|
|
29
|
+
label: String(option)
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function inferAttributeInputKind(spec) {
|
|
34
|
+
if (spec.input)
|
|
35
|
+
return spec.input;
|
|
36
|
+
if (spec.options && spec.options.length > 0)
|
|
37
|
+
return 'select';
|
|
38
|
+
if (typeof spec.default === 'boolean')
|
|
39
|
+
return 'checkbox';
|
|
40
|
+
if (typeof spec.default === 'number')
|
|
41
|
+
return 'number';
|
|
42
|
+
if (Array.isArray(spec.default) || isPlainObject(spec.default))
|
|
43
|
+
return 'json';
|
|
44
|
+
if (typeof spec.default === 'string' && spec.default.includes('\n'))
|
|
45
|
+
return 'textarea';
|
|
46
|
+
return 'text';
|
|
47
|
+
}
|
|
48
|
+
export function coerceAttributeValue(spec, value) {
|
|
49
|
+
if (value === undefined || value === null || value === '') {
|
|
50
|
+
return spec.default;
|
|
51
|
+
}
|
|
52
|
+
if (spec.parse) {
|
|
53
|
+
const parsed = spec.parse(value);
|
|
54
|
+
return parsed === undefined ? spec.default : parsed;
|
|
55
|
+
}
|
|
56
|
+
const fallback = spec.default;
|
|
57
|
+
if (spec.input === 'richtext') {
|
|
58
|
+
return coerceRichTextDocument(value);
|
|
59
|
+
}
|
|
60
|
+
if (typeof fallback === 'string') {
|
|
61
|
+
return String(value);
|
|
62
|
+
}
|
|
63
|
+
if (typeof fallback === 'number') {
|
|
64
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
65
|
+
return value;
|
|
66
|
+
if (typeof value === 'string' && value.trim()) {
|
|
67
|
+
const parsed = Number(value);
|
|
68
|
+
if (Number.isFinite(parsed))
|
|
69
|
+
return parsed;
|
|
70
|
+
}
|
|
71
|
+
return fallback;
|
|
72
|
+
}
|
|
73
|
+
if (typeof fallback === 'boolean') {
|
|
74
|
+
if (typeof value === 'boolean')
|
|
75
|
+
return value;
|
|
76
|
+
if (typeof value === 'string') {
|
|
77
|
+
const normalized = value.trim().toLowerCase();
|
|
78
|
+
if (['true', '1', 'yes', 'on'].includes(normalized))
|
|
79
|
+
return true;
|
|
80
|
+
if (['false', '0', 'no', 'off'].includes(normalized))
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return fallback;
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(fallback)) {
|
|
86
|
+
if (Array.isArray(value))
|
|
87
|
+
return value;
|
|
88
|
+
if (typeof value === 'string' && value.trim()) {
|
|
89
|
+
const parsed = parseJsonValue(value);
|
|
90
|
+
if (Array.isArray(parsed))
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
return fallback;
|
|
94
|
+
}
|
|
95
|
+
if (isPlainObject(fallback)) {
|
|
96
|
+
if (isPlainObject(value))
|
|
97
|
+
return value;
|
|
98
|
+
if (typeof value === 'string' && value.trim()) {
|
|
99
|
+
const parsed = parseJsonValue(value);
|
|
100
|
+
if (isPlainObject(parsed))
|
|
101
|
+
return parsed;
|
|
102
|
+
}
|
|
103
|
+
return fallback;
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
export function normalizeBlockAttributes(block, attrs = {}) {
|
|
108
|
+
return Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
109
|
+
name,
|
|
110
|
+
coerceAttributeValue(spec, attrs[name])
|
|
111
|
+
]));
|
|
112
|
+
}
|
|
113
|
+
export function serializeAttributeValue(spec, value) {
|
|
114
|
+
if (spec.serialize) {
|
|
115
|
+
return spec.serialize(value);
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
export function serializeBlockAttributes(block, attrs) {
|
|
120
|
+
return JSON.stringify(Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
121
|
+
name,
|
|
122
|
+
serializeAttributeValue(spec, coerceAttributeValue(spec, attrs[name]))
|
|
123
|
+
])));
|
|
124
|
+
}
|
|
125
|
+
export function toAttributeDraftValue(spec, value) {
|
|
126
|
+
const normalized = coerceAttributeValue(spec, value);
|
|
127
|
+
const input = inferAttributeInputKind(spec);
|
|
128
|
+
if (input === 'checkbox')
|
|
129
|
+
return Boolean(normalized);
|
|
130
|
+
if (input === 'number')
|
|
131
|
+
return typeof normalized === 'number' ? normalized : spec.default;
|
|
132
|
+
if (input === 'richtext')
|
|
133
|
+
return normalized;
|
|
134
|
+
if (input === 'json')
|
|
135
|
+
return JSON.stringify(normalized, null, 2);
|
|
136
|
+
return String(normalized ?? '');
|
|
137
|
+
}
|
|
138
|
+
export function toAttributeDraftValues(block, attrs) {
|
|
139
|
+
return Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
140
|
+
name,
|
|
141
|
+
toAttributeDraftValue(spec, attrs[name])
|
|
142
|
+
]));
|
|
143
|
+
}
|
|
144
|
+
export function parseBlockDraftAttributes(block, draftAttrs) {
|
|
145
|
+
return Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
146
|
+
name,
|
|
147
|
+
coerceAttributeValue(spec, draftAttrs[name])
|
|
148
|
+
]));
|
|
149
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function isAttributeSpec(value) {
|
|
2
|
+
return typeof value === 'object' && value !== null && 'default' in value;
|
|
3
|
+
}
|
|
4
|
+
function isAttributeOption(value) {
|
|
5
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && 'value' in value;
|
|
6
|
+
}
|
|
7
|
+
function optionValues(options) {
|
|
8
|
+
return options.map((option) => (isAttributeOption(option) ? option.value : option));
|
|
9
|
+
}
|
|
10
|
+
function normalizeAttribute(value) {
|
|
11
|
+
const spec = isAttributeSpec(value) ? { ...value } : { default: value };
|
|
12
|
+
if (spec.options && spec.options.length > 0 && !spec.validate) {
|
|
13
|
+
const allowed = optionValues(spec.options);
|
|
14
|
+
spec.validate = (candidate) => allowed.includes(candidate);
|
|
15
|
+
}
|
|
16
|
+
return spec;
|
|
17
|
+
}
|
|
18
|
+
function normalizeAttributes(attributes) {
|
|
19
|
+
const normalized = {};
|
|
20
|
+
for (const key of Object.keys(attributes)) {
|
|
21
|
+
const value = attributes[key];
|
|
22
|
+
normalized[key] = normalizeAttribute(value);
|
|
23
|
+
}
|
|
24
|
+
return normalized;
|
|
25
|
+
}
|
|
26
|
+
function validateConfig(config) {
|
|
27
|
+
if (!config.id.trim()) {
|
|
28
|
+
throw new Error('Block id must be a non-empty string');
|
|
29
|
+
}
|
|
30
|
+
if (!config.label.trim()) {
|
|
31
|
+
throw new Error(`Block "${config.id}" must have a non-empty label`);
|
|
32
|
+
}
|
|
33
|
+
for (const [name, attr] of Object.entries(config.attributes)) {
|
|
34
|
+
const normalized = normalizeAttribute(attr);
|
|
35
|
+
if (!('default' in normalized)) {
|
|
36
|
+
throw new Error(`Attribute "${name}" in block "${config.id}" must define a default value`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (config.content && config.behaviors?.inline) {
|
|
40
|
+
throw new Error(`Block "${config.id}" cannot be inline when content.kind is "${config.content.kind}"`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function normalizeComponents(config) {
|
|
44
|
+
const shared = config.component;
|
|
45
|
+
const editor = config.components?.editor ?? shared ?? config.components?.render;
|
|
46
|
+
const render = config.components?.render ?? shared ?? config.components?.editor;
|
|
47
|
+
if (!editor || !render) {
|
|
48
|
+
throw new Error(`Block "${config.id}" must provide either "component" or both "components.editor" and "components.render"`);
|
|
49
|
+
}
|
|
50
|
+
return { editor, render };
|
|
51
|
+
}
|
|
52
|
+
function normalizeContent(content) {
|
|
53
|
+
if (!content) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
kind: content.kind
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function defineBlock(config) {
|
|
61
|
+
validateConfig(config);
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
id: config.id,
|
|
64
|
+
label: config.label,
|
|
65
|
+
description: config.description,
|
|
66
|
+
icon: config.icon,
|
|
67
|
+
attributes: normalizeAttributes(config.attributes),
|
|
68
|
+
components: normalizeComponents(config),
|
|
69
|
+
behaviors: {
|
|
70
|
+
inline: config.behaviors?.inline ?? false,
|
|
71
|
+
draggable: config.behaviors?.draggable ?? true,
|
|
72
|
+
selectable: config.behaviors?.selectable ?? true
|
|
73
|
+
},
|
|
74
|
+
content: normalizeContent(config.content),
|
|
75
|
+
html: config.html
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { defineBlock } from './defineBlock.js';
|
|
2
|
+
export { coerceAttributeValue, inferAttributeInputKind, normalizeAttributeOptions, normalizeBlockAttributes, parseBlockDraftAttributes, serializeBlockAttributes, toAttributeDraftValues } from './attributes.js';
|
|
3
|
+
export { createBlockRegistry, createSchema, resolveRegistry } from './registry.js';
|
|
4
|
+
export { CURRENT_DOCUMENT_VERSION, normalizeDocument } from './normalize.js';
|
|
5
|
+
export { validateDocument } from './validate.js';
|
|
6
|
+
export { coerceRichTextDocument, emptyRichTextDocument, hasRichTextContent, isRichTextDocument, richTextDocument, resolveRichTextFeatures } from '../shared/richText.js';
|
|
7
|
+
export type { AttributeConfig, AttributeInputKind, AttributeOption, AttrParser, AttrSerializer, AttributeSpec, BlockAttributes, BlockBehavior, BlockComponentProps, BlockConfig, BlockContentDefinition, BlockContentKind, BlockDefinition, BlockMetadata, BlockRegistry, ContentDocument, ContentSchema, CreateSchemaOptions, RichTextFeature, RichTextFeatureSelection, ValidateDocumentOptions, ValidationCode, ValidationIssue, ValidationResult } from './types.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { defineBlock } from './defineBlock.js';
|
|
2
|
+
export { coerceAttributeValue, inferAttributeInputKind, normalizeAttributeOptions, normalizeBlockAttributes, parseBlockDraftAttributes, serializeBlockAttributes, toAttributeDraftValues } from './attributes.js';
|
|
3
|
+
export { createBlockRegistry, createSchema, resolveRegistry } from './registry.js';
|
|
4
|
+
export { CURRENT_DOCUMENT_VERSION, normalizeDocument } from './normalize.js';
|
|
5
|
+
export { validateDocument } from './validate.js';
|
|
6
|
+
export { coerceRichTextDocument, emptyRichTextDocument, hasRichTextContent, isRichTextDocument, richTextDocument, resolveRichTextFeatures } from '../shared/richText.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PMDoc } from '../shared/document.js';
|
|
2
|
+
import type { ContentSchema } from './types.js';
|
|
3
|
+
import { resolveRegistry } from './registry.js';
|
|
4
|
+
export declare const CURRENT_DOCUMENT_VERSION = 1;
|
|
5
|
+
export declare function normalizeDocument(document: Partial<PMDoc> | null | undefined, blocks: Parameters<typeof resolveRegistry>[0], schema?: ContentSchema): PMDoc;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { resolveRegistry } from './registry.js';
|
|
2
|
+
import { normalizeBlockAttributes } from './attributes.js';
|
|
3
|
+
export const CURRENT_DOCUMENT_VERSION = 1;
|
|
4
|
+
function normalizeMarks(marks, schema) {
|
|
5
|
+
if (!marks)
|
|
6
|
+
return undefined;
|
|
7
|
+
const normalized = marks.filter((mark) => Boolean(mark?.type) && (!schema || schema.allowedMarks.has(mark.type)));
|
|
8
|
+
return normalized.length ? normalized : undefined;
|
|
9
|
+
}
|
|
10
|
+
function normalizeNode(node, registryBlocks, schema) {
|
|
11
|
+
const block = registryBlocks.get(node.type);
|
|
12
|
+
const normalizedContent = Array.isArray(node.content)
|
|
13
|
+
? node.content.map((child) => normalizeNode(child, registryBlocks, schema))
|
|
14
|
+
: undefined;
|
|
15
|
+
if (block) {
|
|
16
|
+
return {
|
|
17
|
+
type: node.type,
|
|
18
|
+
attrs: normalizeBlockAttributes(block, node.attrs ?? {}),
|
|
19
|
+
marks: normalizeMarks(node.marks, schema),
|
|
20
|
+
content: block.content ? normalizedContent : undefined
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
...node,
|
|
25
|
+
marks: normalizeMarks(node.marks, schema),
|
|
26
|
+
content: normalizedContent
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function normalizeDocument(document, blocks, schema) {
|
|
30
|
+
const registry = resolveRegistry(blocks);
|
|
31
|
+
const registryBlocks = new Map(registry.blocks.map((block) => [block.id, block]));
|
|
32
|
+
const content = Array.isArray(document?.content) ? document.content : [];
|
|
33
|
+
return {
|
|
34
|
+
type: 'doc',
|
|
35
|
+
version: CURRENT_DOCUMENT_VERSION,
|
|
36
|
+
content: content.map((node) => normalizeNode(node, registryBlocks, schema))
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions } from './types.js';
|
|
2
|
+
export declare function createBlockRegistry(blocks: BlockDefinition[]): BlockRegistry;
|
|
3
|
+
export declare function createSchema(registry: BlockRegistry, options?: CreateSchemaOptions): ContentSchema;
|
|
4
|
+
export declare function resolveRegistry(blocks: BlockRegistry | BlockDefinition[]): BlockRegistry;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const DEFAULT_MARKS = ['bold', 'italic', 'strike', 'code', 'link'];
|
|
2
|
+
export function createBlockRegistry(blocks) {
|
|
3
|
+
const byId = new Map();
|
|
4
|
+
const metadata = [];
|
|
5
|
+
for (const block of blocks) {
|
|
6
|
+
if (byId.has(block.id)) {
|
|
7
|
+
throw new Error(`Duplicate block id "${block.id}"`);
|
|
8
|
+
}
|
|
9
|
+
byId.set(block.id, block);
|
|
10
|
+
metadata.push({
|
|
11
|
+
id: block.id,
|
|
12
|
+
label: block.label,
|
|
13
|
+
description: block.description,
|
|
14
|
+
icon: block.icon
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
blocks,
|
|
19
|
+
byId,
|
|
20
|
+
metadata,
|
|
21
|
+
get(id) {
|
|
22
|
+
return byId.get(id);
|
|
23
|
+
},
|
|
24
|
+
has(id) {
|
|
25
|
+
return byId.has(id);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function createSchema(registry, options = {}) {
|
|
30
|
+
const allowedBlocks = options.allowedBlocks
|
|
31
|
+
? new Set(options.allowedBlocks.filter((id) => registry.has(id)))
|
|
32
|
+
: new Set(registry.blocks.map((block) => block.id));
|
|
33
|
+
const allowedMarks = new Set(options.allowedMarks ?? DEFAULT_MARKS);
|
|
34
|
+
return {
|
|
35
|
+
allowedBlocks,
|
|
36
|
+
allowedMarks
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function resolveRegistry(blocks) {
|
|
40
|
+
return Array.isArray(blocks) ? createBlockRegistry(blocks) : blocks;
|
|
41
|
+
}
|