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,130 @@
|
|
|
1
|
+
import type { Component, Snippet } from 'svelte';
|
|
2
|
+
import type { PMDoc, PMPath } from '../shared/document.js';
|
|
3
|
+
export type AttrValidator<T> = (value: unknown) => value is T;
|
|
4
|
+
export type AttrParser<T> = (value: unknown) => T | undefined;
|
|
5
|
+
export type AttrSerializer = (value: unknown) => unknown;
|
|
6
|
+
export type AttributeInputKind = 'text' | 'textarea' | 'number' | 'checkbox' | 'json' | 'richtext' | 'select';
|
|
7
|
+
export interface AttributeOption<T> {
|
|
8
|
+
value: T;
|
|
9
|
+
label?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
}
|
|
12
|
+
export type RichTextFeature = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'heading' | 'bulletList' | 'orderedList' | 'blockquote' | 'codeBlock' | 'horizontalRule' | 'hardBreak';
|
|
13
|
+
export type RichTextFeatureSelection = '*' | '__all__' | RichTextFeature[];
|
|
14
|
+
export interface AttributeSpec<T> {
|
|
15
|
+
default: T;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
validate?: AttrValidator<T> | ((value: unknown) => boolean);
|
|
18
|
+
parse?: AttrParser<T>;
|
|
19
|
+
serialize?: AttrSerializer;
|
|
20
|
+
input?: AttributeInputKind;
|
|
21
|
+
placeholder?: string;
|
|
22
|
+
options?: ReadonlyArray<T | AttributeOption<T>>;
|
|
23
|
+
richText?: {
|
|
24
|
+
features?: RichTextFeatureSelection;
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export type AttributeConfig<T> = AttributeSpec<T> | T;
|
|
29
|
+
export type BlockAttributes = Record<string, unknown>;
|
|
30
|
+
export type HtmlSpecValue = string | number | boolean | null | undefined;
|
|
31
|
+
export type HtmlSpec = [string, Record<string, unknown>?, ...(HtmlSpec | HtmlSpecValue)[]];
|
|
32
|
+
export type BlockHtmlRenderer = (attrs: BlockAttributes) => HtmlSpec;
|
|
33
|
+
export type BlockContentKind = 'flow';
|
|
34
|
+
export interface BlockContentDefinition {
|
|
35
|
+
kind: BlockContentKind;
|
|
36
|
+
}
|
|
37
|
+
export interface BlockComponentProps {
|
|
38
|
+
content?: import('../shared/document.js').PMNode[];
|
|
39
|
+
children?: Snippet;
|
|
40
|
+
[name: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
export type NormalizedAttributes<Attrs extends BlockAttributes> = {
|
|
43
|
+
[K in keyof Attrs]: AttributeSpec<Attrs[K]>;
|
|
44
|
+
};
|
|
45
|
+
export interface BlockComponents {
|
|
46
|
+
editor: Component<Record<string, unknown>>;
|
|
47
|
+
render: Component<Record<string, unknown>>;
|
|
48
|
+
}
|
|
49
|
+
export type BlockComponent = Component<Record<string, unknown>>;
|
|
50
|
+
export interface BlockBehavior {
|
|
51
|
+
inline?: boolean;
|
|
52
|
+
draggable?: boolean;
|
|
53
|
+
selectable?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface BlockConfig<Attrs extends BlockAttributes> {
|
|
56
|
+
id: string;
|
|
57
|
+
label: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
icon?: string | Component<Record<string, never>>;
|
|
60
|
+
attributes: {
|
|
61
|
+
[K in keyof Attrs]: AttributeConfig<Attrs[K]>;
|
|
62
|
+
};
|
|
63
|
+
component?: BlockComponent;
|
|
64
|
+
components?: Partial<BlockComponents>;
|
|
65
|
+
behaviors?: BlockBehavior;
|
|
66
|
+
content?: false | BlockContentDefinition;
|
|
67
|
+
html?: {
|
|
68
|
+
parseTag?: string;
|
|
69
|
+
render?: BlockHtmlRenderer;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export interface BlockDefinition<Attrs extends BlockAttributes = BlockAttributes> {
|
|
73
|
+
id: string;
|
|
74
|
+
label: string;
|
|
75
|
+
description?: string;
|
|
76
|
+
icon?: string | Component<Record<string, never>>;
|
|
77
|
+
attributes: NormalizedAttributes<Attrs>;
|
|
78
|
+
components: BlockComponents;
|
|
79
|
+
behaviors: BlockBehavior;
|
|
80
|
+
content?: BlockContentDefinition;
|
|
81
|
+
html?: {
|
|
82
|
+
parseTag?: string;
|
|
83
|
+
render?: BlockHtmlRenderer;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export interface BlockMetadata {
|
|
87
|
+
id: string;
|
|
88
|
+
label: string;
|
|
89
|
+
description?: string;
|
|
90
|
+
icon?: string | Component<Record<string, never>>;
|
|
91
|
+
}
|
|
92
|
+
export interface BlockRegistry {
|
|
93
|
+
blocks: BlockDefinition[];
|
|
94
|
+
byId: ReadonlyMap<string, BlockDefinition>;
|
|
95
|
+
metadata: BlockMetadata[];
|
|
96
|
+
get(id: string): BlockDefinition | undefined;
|
|
97
|
+
has(id: string): boolean;
|
|
98
|
+
}
|
|
99
|
+
export interface ContentSchema {
|
|
100
|
+
allowedBlocks: ReadonlySet<string>;
|
|
101
|
+
allowedMarks: ReadonlySet<string>;
|
|
102
|
+
}
|
|
103
|
+
export type ValidationCode = 'UNKNOWN_BLOCK' | 'INVALID_ATTR' | 'INVALID_CONTENT' | 'DISALLOWED_BLOCK' | 'DISALLOWED_MARK' | 'MALFORMED_NODE';
|
|
104
|
+
export interface ValidationIssue {
|
|
105
|
+
code: ValidationCode;
|
|
106
|
+
path: PMPath;
|
|
107
|
+
message: string;
|
|
108
|
+
severity: 'warning' | 'error';
|
|
109
|
+
details?: Record<string, unknown>;
|
|
110
|
+
}
|
|
111
|
+
export interface ValidationResult {
|
|
112
|
+
ok: boolean;
|
|
113
|
+
issues: ValidationIssue[];
|
|
114
|
+
}
|
|
115
|
+
export interface CreateSchemaOptions {
|
|
116
|
+
allowedBlocks?: string[];
|
|
117
|
+
allowedMarks?: string[];
|
|
118
|
+
}
|
|
119
|
+
export interface ValidateDocumentOptions {
|
|
120
|
+
onIssue?: (issue: ValidationIssue) => void;
|
|
121
|
+
}
|
|
122
|
+
export interface EditorIssueEvent {
|
|
123
|
+
issue: ValidationIssue;
|
|
124
|
+
source: 'editor' | 'render';
|
|
125
|
+
}
|
|
126
|
+
export interface EditorOptions {
|
|
127
|
+
allowedMarks?: string[];
|
|
128
|
+
onIssue?: (issue: ValidationIssue) => void;
|
|
129
|
+
}
|
|
130
|
+
export type ContentDocument = PMDoc;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PMDoc } from '../shared/document.js';
|
|
2
|
+
import type { ContentSchema, ValidateDocumentOptions, ValidationResult } from './types.js';
|
|
3
|
+
import { resolveRegistry } from './registry.js';
|
|
4
|
+
export declare function validateDocument(document: PMDoc, blocks: Parameters<typeof resolveRegistry>[0], schema: ContentSchema, options?: ValidateDocumentOptions): ValidationResult;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { resolveRegistry } from './registry.js';
|
|
2
|
+
const BUILTIN_NODE_TYPES = new Set([
|
|
3
|
+
'doc',
|
|
4
|
+
'paragraph',
|
|
5
|
+
'text',
|
|
6
|
+
'heading',
|
|
7
|
+
'blockquote',
|
|
8
|
+
'codeBlock',
|
|
9
|
+
'horizontalRule',
|
|
10
|
+
'hardBreak',
|
|
11
|
+
'bulletList',
|
|
12
|
+
'orderedList',
|
|
13
|
+
'listItem'
|
|
14
|
+
]);
|
|
15
|
+
function pushIssue(issues, options, issue) {
|
|
16
|
+
issues.push(issue);
|
|
17
|
+
options?.onIssue?.(issue);
|
|
18
|
+
}
|
|
19
|
+
function validateMarks(marks, path, schema, issues, options) {
|
|
20
|
+
if (!marks)
|
|
21
|
+
return;
|
|
22
|
+
marks.forEach((mark, markIndex) => {
|
|
23
|
+
if (!mark?.type || typeof mark.type !== 'string') {
|
|
24
|
+
pushIssue(issues, options, {
|
|
25
|
+
code: 'MALFORMED_NODE',
|
|
26
|
+
path: [...path, 'marks', markIndex],
|
|
27
|
+
message: 'Mark is malformed',
|
|
28
|
+
severity: 'error'
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!schema.allowedMarks.has(mark.type)) {
|
|
33
|
+
pushIssue(issues, options, {
|
|
34
|
+
code: 'DISALLOWED_MARK',
|
|
35
|
+
path: [...path, 'marks', markIndex],
|
|
36
|
+
message: `Mark "${mark.type}" is not allowed by this schema`,
|
|
37
|
+
severity: 'error',
|
|
38
|
+
details: { mark: mark.type }
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function validateBlockAttrs(node, block, path, issues, options) {
|
|
44
|
+
const attrs = node.attrs ?? {};
|
|
45
|
+
for (const [name, spec] of Object.entries(block.attributes)) {
|
|
46
|
+
const value = attrs[name];
|
|
47
|
+
const missing = value === undefined || value === null;
|
|
48
|
+
if (missing && spec.required) {
|
|
49
|
+
pushIssue(issues, options, {
|
|
50
|
+
code: 'INVALID_ATTR',
|
|
51
|
+
path: [...path, 'attrs', name],
|
|
52
|
+
message: `Required attribute "${name}" is missing for block "${block.id}"`,
|
|
53
|
+
severity: 'error'
|
|
54
|
+
});
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (missing)
|
|
58
|
+
continue;
|
|
59
|
+
if (spec.validate && !spec.validate(value)) {
|
|
60
|
+
pushIssue(issues, options, {
|
|
61
|
+
code: 'INVALID_ATTR',
|
|
62
|
+
path: [...path, 'attrs', name],
|
|
63
|
+
message: `Attribute "${name}" is invalid for block "${block.id}"`,
|
|
64
|
+
severity: 'error',
|
|
65
|
+
details: { value }
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function validateBlockContent(node, block, path, issues, options) {
|
|
71
|
+
if (node.content === undefined) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
if (!Array.isArray(node.content)) {
|
|
75
|
+
pushIssue(issues, options, {
|
|
76
|
+
code: 'INVALID_CONTENT',
|
|
77
|
+
path: [...path, 'content'],
|
|
78
|
+
message: `Block "${block.id}" content must be an array when present`,
|
|
79
|
+
severity: 'error'
|
|
80
|
+
});
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
if (!block.content) {
|
|
84
|
+
pushIssue(issues, options, {
|
|
85
|
+
code: 'INVALID_CONTENT',
|
|
86
|
+
path: [...path, 'content'],
|
|
87
|
+
message: `Atomic block "${block.id}" cannot contain child content`,
|
|
88
|
+
severity: 'error'
|
|
89
|
+
});
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
function validateNode(node, path, registryBlocks, schema, issues, options) {
|
|
95
|
+
if (!node || typeof node.type !== 'string') {
|
|
96
|
+
pushIssue(issues, options, {
|
|
97
|
+
code: 'MALFORMED_NODE',
|
|
98
|
+
path,
|
|
99
|
+
message: 'Node is malformed',
|
|
100
|
+
severity: 'error'
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
validateMarks(node.marks, path, schema, issues, options);
|
|
105
|
+
const block = registryBlocks.get(node.type);
|
|
106
|
+
const isKnownBuiltin = BUILTIN_NODE_TYPES.has(node.type);
|
|
107
|
+
if (block) {
|
|
108
|
+
if (!schema.allowedBlocks.has(block.id)) {
|
|
109
|
+
pushIssue(issues, options, {
|
|
110
|
+
code: 'DISALLOWED_BLOCK',
|
|
111
|
+
path,
|
|
112
|
+
message: `Block "${block.id}" is not allowed by this schema`,
|
|
113
|
+
severity: 'error',
|
|
114
|
+
details: { block: block.id }
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
validateBlockAttrs(node, block, path, issues, options);
|
|
118
|
+
if (!validateBlockContent(node, block, path, issues, options)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (!isKnownBuiltin) {
|
|
123
|
+
pushIssue(issues, options, {
|
|
124
|
+
code: 'UNKNOWN_BLOCK',
|
|
125
|
+
path,
|
|
126
|
+
message: `Unknown block/node type "${node.type}"`,
|
|
127
|
+
severity: 'warning',
|
|
128
|
+
details: { block: node.type }
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (node.type === 'text' && typeof node.text !== 'string') {
|
|
132
|
+
pushIssue(issues, options, {
|
|
133
|
+
code: 'MALFORMED_NODE',
|
|
134
|
+
path: [...path, 'text'],
|
|
135
|
+
message: 'Text node is missing text value',
|
|
136
|
+
severity: 'error'
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (!block && node.content && !Array.isArray(node.content)) {
|
|
140
|
+
pushIssue(issues, options, {
|
|
141
|
+
code: 'MALFORMED_NODE',
|
|
142
|
+
path: [...path, 'content'],
|
|
143
|
+
message: 'Node content must be an array when present',
|
|
144
|
+
severity: 'error'
|
|
145
|
+
});
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
node.content?.forEach((child, index) => {
|
|
149
|
+
validateNode(child, [...path, 'content', index], registryBlocks, schema, issues, options);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
export function validateDocument(document, blocks, schema, options) {
|
|
153
|
+
const registry = resolveRegistry(blocks);
|
|
154
|
+
const issues = [];
|
|
155
|
+
if (!document || document.type !== 'doc') {
|
|
156
|
+
pushIssue(issues, options, {
|
|
157
|
+
code: 'MALFORMED_NODE',
|
|
158
|
+
path: [],
|
|
159
|
+
message: 'Document root must be a ProseMirror doc node',
|
|
160
|
+
severity: 'error'
|
|
161
|
+
});
|
|
162
|
+
return { ok: false, issues };
|
|
163
|
+
}
|
|
164
|
+
if (document.content && !Array.isArray(document.content)) {
|
|
165
|
+
pushIssue(issues, options, {
|
|
166
|
+
code: 'MALFORMED_NODE',
|
|
167
|
+
path: ['content'],
|
|
168
|
+
message: 'Document content must be an array when present',
|
|
169
|
+
severity: 'error'
|
|
170
|
+
});
|
|
171
|
+
return { ok: false, issues };
|
|
172
|
+
}
|
|
173
|
+
const registryBlocks = new Map(registry.blocks.map((block) => [block.id, block]));
|
|
174
|
+
document.content?.forEach((node, index) => {
|
|
175
|
+
validateNode(node, ['content', index], registryBlocks, schema, issues, options);
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
ok: !issues.some((issue) => issue.severity === 'error'),
|
|
179
|
+
issues
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { inferAttributeInputKind, normalizeAttributeOptions } from '../core/attributes.js';
|
|
3
|
+
import { resolveRegistry } from '../core/registry.js';
|
|
4
|
+
import type { AttributeSpec, BlockDefinition, BlockRegistry } from '../core/types.js';
|
|
5
|
+
import { CODE_BLOCK_ID, codeBlockAttributeTarget } from '../shared/codeBlockAttributes.js';
|
|
6
|
+
import type { BlockAttributesController, BlockAttributesState } from './attributesController.js';
|
|
7
|
+
import { flip } from 'svelte/animate';
|
|
8
|
+
import ArrowUpIcon from 'phosphor-svelte/lib/ArrowUpIcon';
|
|
9
|
+
import ArrowDownIcon from 'phosphor-svelte/lib/ArrowDownIcon';
|
|
10
|
+
import CaretDownIcon from 'phosphor-svelte/lib/CaretDownIcon';
|
|
11
|
+
import DotsSixVerticalIcon from 'phosphor-svelte/lib/DotsSixVerticalIcon';
|
|
12
|
+
import PlusIcon from 'phosphor-svelte/lib/PlusIcon';
|
|
13
|
+
import TrashIcon from 'phosphor-svelte/lib/TrashIcon';
|
|
14
|
+
import RichTextAttributeEditor from './RichTextAttributeEditor.svelte';
|
|
15
|
+
import LinkAttributesPanel from './LinkAttributesPanel.svelte';
|
|
16
|
+
|
|
17
|
+
interface Props {
|
|
18
|
+
controller: BlockAttributesController;
|
|
19
|
+
blocks?: BlockRegistry | BlockDefinition[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let { controller, blocks = [] }: Props = $props();
|
|
23
|
+
|
|
24
|
+
let controllerState = $state<BlockAttributesState>({
|
|
25
|
+
open: false,
|
|
26
|
+
mode: null,
|
|
27
|
+
selectedBlockId: '',
|
|
28
|
+
draftAttrs: {},
|
|
29
|
+
validationErrors: {},
|
|
30
|
+
activeBlock: null,
|
|
31
|
+
allowedBlockIds: [],
|
|
32
|
+
containerChildren: [],
|
|
33
|
+
link: {
|
|
34
|
+
open: false,
|
|
35
|
+
attrs: {}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const registry = $derived(resolveRegistry(blocks));
|
|
40
|
+
const selectedBlock = $derived.by(() => {
|
|
41
|
+
if (controllerState.selectedBlockId === CODE_BLOCK_ID) return codeBlockAttributeTarget;
|
|
42
|
+
return controllerState.selectedBlockId
|
|
43
|
+
? (registry.get(controllerState.selectedBlockId) ?? null)
|
|
44
|
+
: null;
|
|
45
|
+
});
|
|
46
|
+
const selectedAttributeSpecs = $derived(
|
|
47
|
+
selectedBlock
|
|
48
|
+
? (Object.entries(selectedBlock.attributes) as Array<[string, AttributeSpec<unknown>]>)
|
|
49
|
+
: []
|
|
50
|
+
);
|
|
51
|
+
let childBlockQuery = $state('');
|
|
52
|
+
const hasChildren = $derived(controllerState.containerChildren.length > 0);
|
|
53
|
+
const canRemoveChild = $derived(controllerState.containerChildren.length > 1);
|
|
54
|
+
const activeBlocks = $derived.by(() =>
|
|
55
|
+
registry.blocks.filter((block) =>
|
|
56
|
+
controllerState.allowedBlockIds.length
|
|
57
|
+
? controllerState.allowedBlockIds.includes(block.id)
|
|
58
|
+
: true
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
const canAddNestedBlock = $derived(
|
|
62
|
+
controllerState.mode === 'edit' &&
|
|
63
|
+
Boolean(controllerState.activeBlock) &&
|
|
64
|
+
Boolean(selectedBlock && 'content' in selectedBlock && selectedBlock.content)
|
|
65
|
+
);
|
|
66
|
+
const filteredChildBlocks = $derived.by(() => {
|
|
67
|
+
const query = childBlockQuery.trim().toLowerCase();
|
|
68
|
+
return activeBlocks.filter((block) => !query || block.label.toLowerCase().includes(query));
|
|
69
|
+
});
|
|
70
|
+
let draggingChildIndex = $state<number | null>(null);
|
|
71
|
+
let draggingPointerId = $state<number | null>(null);
|
|
72
|
+
|
|
73
|
+
function getDraftStringValue(name: string): string {
|
|
74
|
+
const value = controllerState.draftAttrs[name];
|
|
75
|
+
if (typeof value === 'string') return value;
|
|
76
|
+
if (value === undefined || value === null) return '';
|
|
77
|
+
return String(value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function moveUp(index: number): void {
|
|
81
|
+
if (index > 0) controller.moveContainerChild(index, index - 1);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function moveDown(index: number): void {
|
|
85
|
+
if (index < controllerState.containerChildren.length - 1) {
|
|
86
|
+
controller.moveContainerChild(index, index + 1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function addNestedBlock(blockId: string): void {
|
|
91
|
+
if (controller.insertContainerChild(blockId)) {
|
|
92
|
+
childBlockQuery = '';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function startChildDrag(event: PointerEvent, index: number): void {
|
|
97
|
+
event.stopPropagation();
|
|
98
|
+
draggingChildIndex = index;
|
|
99
|
+
draggingPointerId = event.pointerId;
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function handleChildDragMove(event: PointerEvent): void {
|
|
104
|
+
if (draggingChildIndex === null || draggingPointerId !== event.pointerId) return;
|
|
105
|
+
const target = document
|
|
106
|
+
.elementFromPoint(event.clientX, event.clientY)
|
|
107
|
+
?.closest('[data-child-index]');
|
|
108
|
+
if (!(target instanceof HTMLElement)) return;
|
|
109
|
+
const targetIndex = Number(target.dataset.childIndex);
|
|
110
|
+
if (!Number.isInteger(targetIndex) || targetIndex === draggingChildIndex) return;
|
|
111
|
+
if (controller.moveContainerChild(draggingChildIndex, targetIndex)) {
|
|
112
|
+
draggingChildIndex = targetIndex;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function stopChildDrag(event: PointerEvent): void {
|
|
117
|
+
if (draggingPointerId !== event.pointerId) return;
|
|
118
|
+
draggingChildIndex = null;
|
|
119
|
+
draggingPointerId = null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
$effect(() => {
|
|
123
|
+
const unsubscribe = controller.subscribe((state) => {
|
|
124
|
+
controllerState = state;
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return unsubscribe;
|
|
128
|
+
});
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<svelte:document
|
|
132
|
+
onpointermove={handleChildDragMove}
|
|
133
|
+
onpointerup={stopChildDrag}
|
|
134
|
+
onpointercancel={stopChildDrag}
|
|
135
|
+
/>
|
|
136
|
+
|
|
137
|
+
<div class="uncial-attrs-panel">
|
|
138
|
+
{#if controllerState.link.open}
|
|
139
|
+
<LinkAttributesPanel
|
|
140
|
+
attrs={controllerState.link.attrs}
|
|
141
|
+
onChange={(name, value) => controller.setLinkAttr(name, value)}
|
|
142
|
+
onApply={() => controller.commitLinkAttributes()}
|
|
143
|
+
onRemove={() => controller.removeLink()}
|
|
144
|
+
onClose={() => controller.closeLinkAttributes()}
|
|
145
|
+
/>
|
|
146
|
+
{:else if controllerState.open && selectedBlock}
|
|
147
|
+
<p class="uncial-attrs-title text-sm font-bold">
|
|
148
|
+
{controllerState.mode === 'edit' ? 'Edit' : 'Configure'}
|
|
149
|
+
{selectedBlock.label || controllerState.selectedBlockId || 'block'}
|
|
150
|
+
</p>
|
|
151
|
+
{#each selectedAttributeSpecs as [name, spec] (name)}
|
|
152
|
+
{@const inputKind = inferAttributeInputKind(spec)}
|
|
153
|
+
<div class="form-control mb-3 grid gap-1">
|
|
154
|
+
<span class="label-text text-xs font-semibold uppercase tracking-wide opacity-70"
|
|
155
|
+
>{name}</span
|
|
156
|
+
>
|
|
157
|
+
{#if inputKind === 'checkbox'}
|
|
158
|
+
<input
|
|
159
|
+
class="checkbox checkbox-sm"
|
|
160
|
+
type="checkbox"
|
|
161
|
+
checked={Boolean(controllerState.draftAttrs[name])}
|
|
162
|
+
onchange={(event) => {
|
|
163
|
+
const target = event.currentTarget as HTMLInputElement;
|
|
164
|
+
controller.setDraftAttr(name, target.checked);
|
|
165
|
+
}}
|
|
166
|
+
/>
|
|
167
|
+
{:else if inputKind === 'number'}
|
|
168
|
+
<input
|
|
169
|
+
class="input input-bordered input-sm"
|
|
170
|
+
type="number"
|
|
171
|
+
placeholder={spec.placeholder ?? name}
|
|
172
|
+
value={controllerState.draftAttrs[name] ?? ''}
|
|
173
|
+
oninput={(event) => {
|
|
174
|
+
const target = event.currentTarget as HTMLInputElement;
|
|
175
|
+
controller.setDraftAttr(name, target.value === '' ? '' : target.valueAsNumber);
|
|
176
|
+
}}
|
|
177
|
+
/>
|
|
178
|
+
{:else if inputKind === 'richtext'}
|
|
179
|
+
<RichTextAttributeEditor
|
|
180
|
+
value={controllerState.draftAttrs[name]}
|
|
181
|
+
features={spec.richText?.features}
|
|
182
|
+
placeholder={spec.richText?.placeholder ?? spec.placeholder ?? name}
|
|
183
|
+
onChange={(value) => controller.setDraftAttr(name, value)}
|
|
184
|
+
/>
|
|
185
|
+
{:else if inputKind === 'select'}
|
|
186
|
+
{@const options = normalizeAttributeOptions(spec) ?? []}
|
|
187
|
+
<select
|
|
188
|
+
class="select select-bordered select-sm"
|
|
189
|
+
aria-label={name}
|
|
190
|
+
value={getDraftStringValue(name)}
|
|
191
|
+
onchange={(event) => {
|
|
192
|
+
const target = event.currentTarget as HTMLSelectElement;
|
|
193
|
+
controller.setDraftAttr(name, target.value);
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
{#each options as option (String(option.value))}
|
|
197
|
+
<option value={String(option.value)}>{option.label ?? String(option.value)}</option>
|
|
198
|
+
{/each}
|
|
199
|
+
</select>
|
|
200
|
+
{:else if inputKind === 'textarea' || inputKind === 'json'}
|
|
201
|
+
<textarea
|
|
202
|
+
class="textarea textarea-bordered min-h-28"
|
|
203
|
+
placeholder={spec.placeholder ?? name}
|
|
204
|
+
spellcheck={inputKind !== 'json'}
|
|
205
|
+
value={getDraftStringValue(name)}
|
|
206
|
+
oninput={(event) => {
|
|
207
|
+
const target = event.currentTarget as HTMLTextAreaElement;
|
|
208
|
+
controller.setDraftAttr(name, target.value);
|
|
209
|
+
}}
|
|
210
|
+
></textarea>
|
|
211
|
+
{:else}
|
|
212
|
+
<input
|
|
213
|
+
class="input input-bordered input-sm"
|
|
214
|
+
type="text"
|
|
215
|
+
placeholder={spec.placeholder ?? name}
|
|
216
|
+
value={getDraftStringValue(name)}
|
|
217
|
+
oninput={(event) => {
|
|
218
|
+
const target = event.currentTarget as HTMLInputElement;
|
|
219
|
+
controller.setDraftAttr(name, target.value);
|
|
220
|
+
}}
|
|
221
|
+
/>
|
|
222
|
+
{/if}
|
|
223
|
+
{#if controllerState.validationErrors[name]}
|
|
224
|
+
<span class="text-xs text-error">{controllerState.validationErrors[name]}</span>
|
|
225
|
+
{/if}
|
|
226
|
+
</div>
|
|
227
|
+
{/each}
|
|
228
|
+
<div
|
|
229
|
+
class="sticky -bottom-4 flex flex-wrap items-center justify-between gap-2 bg-base-100 pt-3"
|
|
230
|
+
>
|
|
231
|
+
{#if controllerState.mode === 'edit'}
|
|
232
|
+
<button
|
|
233
|
+
type="button"
|
|
234
|
+
class="btn btn-error btn-outline btn-sm"
|
|
235
|
+
onclick={() => controller.removeActiveBlock()}
|
|
236
|
+
>
|
|
237
|
+
Remove Block
|
|
238
|
+
</button>
|
|
239
|
+
{/if}
|
|
240
|
+
{#if controllerState.mode !== 'edit' && controllerState.selectedBlockId !== CODE_BLOCK_ID}
|
|
241
|
+
<button type="button" class="btn btn-primary btn-sm" onclick={() => controller.commit()}>
|
|
242
|
+
Insert Block
|
|
243
|
+
</button>
|
|
244
|
+
{/if}
|
|
245
|
+
</div>
|
|
246
|
+
{:else}
|
|
247
|
+
<p class="text-sm opacity-60">Select a block to edit its attributes.</p>
|
|
248
|
+
{/if}
|
|
249
|
+
|
|
250
|
+
{#if !controllerState.link.open && canAddNestedBlock}
|
|
251
|
+
<div class="uncial-children-section mt-6 border-t border-base-300 pt-4">
|
|
252
|
+
<div class="mb-3 flex items-center justify-between gap-2">
|
|
253
|
+
<p class="text-xs font-bold uppercase tracking-wide opacity-60">Nested blocks</p>
|
|
254
|
+
{#if activeBlocks.length > 0}
|
|
255
|
+
<details class="dropdown dropdown-end">
|
|
256
|
+
<summary class="btn btn-primary btn-xs gap-1">
|
|
257
|
+
<PlusIcon size={12} weight="bold" />
|
|
258
|
+
<span>Add block</span>
|
|
259
|
+
<CaretDownIcon size={10} weight="bold" />
|
|
260
|
+
</summary>
|
|
261
|
+
<div
|
|
262
|
+
class="dropdown-content z-20 mt-2 w-56 rounded-box border border-base-300 bg-base-100 p-2 shadow-xl"
|
|
263
|
+
>
|
|
264
|
+
<input
|
|
265
|
+
class="input input-bordered input-xs mb-2 w-full"
|
|
266
|
+
type="search"
|
|
267
|
+
placeholder="Filter blocks..."
|
|
268
|
+
bind:value={childBlockQuery}
|
|
269
|
+
/>
|
|
270
|
+
<ul class="menu max-h-64 overflow-auto p-0">
|
|
271
|
+
{#each filteredChildBlocks as block (block.id)}
|
|
272
|
+
<li>
|
|
273
|
+
<button type="button" onclick={() => addNestedBlock(block.id)}>
|
|
274
|
+
{block.label}
|
|
275
|
+
</button>
|
|
276
|
+
</li>
|
|
277
|
+
{:else}
|
|
278
|
+
<li class="px-3 py-2 text-xs opacity-60">No matching blocks</li>
|
|
279
|
+
{/each}
|
|
280
|
+
</ul>
|
|
281
|
+
</div>
|
|
282
|
+
</details>
|
|
283
|
+
{/if}
|
|
284
|
+
</div>
|
|
285
|
+
{#if hasChildren}
|
|
286
|
+
<ul class="uncial-children-list flex flex-col gap-1.5">
|
|
287
|
+
{#each controllerState.containerChildren as child, index (child.key)}
|
|
288
|
+
<li
|
|
289
|
+
class="uncial-child-item flex items-center gap-2 rounded-lg border border-base-300 bg-base-200/50 px-2 py-2"
|
|
290
|
+
class:opacity-60={draggingChildIndex === index}
|
|
291
|
+
data-child-index={index}
|
|
292
|
+
animate:flip={{ duration: 120 }}
|
|
293
|
+
>
|
|
294
|
+
<button
|
|
295
|
+
type="button"
|
|
296
|
+
class="btn btn-ghost btn-xs btn-square cursor-grab"
|
|
297
|
+
aria-label="Drag nested block"
|
|
298
|
+
onpointerdown={(event) => startChildDrag(event, index)}
|
|
299
|
+
>
|
|
300
|
+
<DotsSixVerticalIcon size={12} weight="bold" />
|
|
301
|
+
</button>
|
|
302
|
+
<div class="min-w-0 flex-1">
|
|
303
|
+
<span class="text-xs font-bold">{child.label}</span>
|
|
304
|
+
{#if child.summary}
|
|
305
|
+
<span class="ml-1 text-xs opacity-50">{child.summary}</span>
|
|
306
|
+
{/if}
|
|
307
|
+
</div>
|
|
308
|
+
<div class="flex gap-0.5">
|
|
309
|
+
<button
|
|
310
|
+
type="button"
|
|
311
|
+
class="btn btn-ghost btn-xs btn-square"
|
|
312
|
+
aria-label="Move up"
|
|
313
|
+
disabled={index === 0}
|
|
314
|
+
onclick={(event) => {
|
|
315
|
+
event.stopPropagation();
|
|
316
|
+
moveUp(index);
|
|
317
|
+
}}
|
|
318
|
+
>
|
|
319
|
+
<ArrowUpIcon size={12} weight="bold" />
|
|
320
|
+
</button>
|
|
321
|
+
<button
|
|
322
|
+
type="button"
|
|
323
|
+
class="btn btn-ghost btn-xs btn-square"
|
|
324
|
+
aria-label="Move down"
|
|
325
|
+
disabled={index === controllerState.containerChildren.length - 1}
|
|
326
|
+
onclick={(event) => {
|
|
327
|
+
event.stopPropagation();
|
|
328
|
+
moveDown(index);
|
|
329
|
+
}}
|
|
330
|
+
>
|
|
331
|
+
<ArrowDownIcon size={12} weight="bold" />
|
|
332
|
+
</button>
|
|
333
|
+
<button
|
|
334
|
+
type="button"
|
|
335
|
+
class="btn btn-ghost btn-xs btn-square text-error"
|
|
336
|
+
aria-label="Remove nested block"
|
|
337
|
+
disabled={!canRemoveChild}
|
|
338
|
+
onclick={(event) => {
|
|
339
|
+
event.stopPropagation();
|
|
340
|
+
controller.removeContainerChild(index);
|
|
341
|
+
}}
|
|
342
|
+
>
|
|
343
|
+
<TrashIcon size={12} weight="bold" />
|
|
344
|
+
</button>
|
|
345
|
+
</div>
|
|
346
|
+
</li>
|
|
347
|
+
{/each}
|
|
348
|
+
</ul>
|
|
349
|
+
{:else}
|
|
350
|
+
<p class="text-xs opacity-60">No nested blocks yet.</p>
|
|
351
|
+
{/if}
|
|
352
|
+
</div>
|
|
353
|
+
{/if}
|
|
354
|
+
</div>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { BlockDefinition, BlockRegistry } from '../core/types.js';
|
|
2
|
+
import type { BlockAttributesController } from './attributesController.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
controller: BlockAttributesController;
|
|
5
|
+
blocks?: BlockRegistry | BlockDefinition[];
|
|
6
|
+
}
|
|
7
|
+
declare const BlockAttributesPanel: import("svelte").Component<Props, {}, "">;
|
|
8
|
+
type BlockAttributesPanel = ReturnType<typeof BlockAttributesPanel>;
|
|
9
|
+
export default BlockAttributesPanel;
|