@astl/editor-tiptap 0.0.0 → 0.1.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/CHANGELOG.md +111 -0
- package/LICENSE +202 -0
- package/README.md +49 -0
- package/dist/adapter.d.ts +13 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +421 -0
- package/dist/adapter.js.map +1 -0
- package/dist/blocks.d.ts +96 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +460 -0
- package/dist/blocks.js.map +1 -0
- package/dist/define.d.ts +64 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +20 -0
- package/dist/define.js.map +1 -0
- package/dist/element.d.ts +91 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +779 -0
- package/dist/element.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/layouts.d.ts +52 -0
- package/dist/layouts.d.ts.map +1 -0
- package/dist/layouts.js +198 -0
- package/dist/layouts.js.map +1 -0
- package/dist/nodes.d.ts +150 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +2426 -0
- package/dist/nodes.js.map +1 -0
- package/dist/source-view.d.ts +26 -0
- package/dist/source-view.d.ts.map +1 -0
- package/dist/source-view.js +114 -0
- package/dist/source-view.js.map +1 -0
- package/dist/source.d.ts +8 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +24 -0
- package/dist/source.js.map +1 -0
- package/dist/style.d.ts +16 -0
- package/dist/style.d.ts.map +1 -0
- package/dist/style.js +568 -0
- package/dist/style.js.map +1 -0
- package/dist/styles.d.ts +5 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +921 -0
- package/dist/styles.js.map +1 -0
- package/dist/surface.d.ts +24 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +90 -0
- package/dist/surface.js.map +1 -0
- package/dist/tiptap.d.ts +10 -0
- package/dist/tiptap.d.ts.map +1 -0
- package/dist/tiptap.js +9 -0
- package/dist/tiptap.js.map +1 -0
- package/package.json +63 -4
- package/src/adapter.ts +421 -0
- package/src/blocks.ts +577 -0
- package/src/define.ts +84 -0
- package/src/element.ts +988 -0
- package/src/index.ts +10 -0
- package/src/layouts.ts +220 -0
- package/src/nodes.ts +2690 -0
- package/src/source-view.ts +138 -0
- package/src/source.ts +27 -0
- package/src/style.ts +638 -0
- package/src/styles.ts +931 -0
- package/src/surface.ts +110 -0
- package/src/tiptap.ts +9 -0
package/src/style.ts
ADDED
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `EditorAdapter.style` over TipTap: reading and writing `style` and `class` on what the cursor is in, and the
|
|
3
|
+
* document's rules. A mark holds the style of text, a node's attributes that of a block or an object. Everything goes
|
|
4
|
+
* through the allow-list, so nothing refused ever reaches the document.
|
|
5
|
+
*/
|
|
6
|
+
import type {
|
|
7
|
+
EditorStyleChanges,
|
|
8
|
+
EditorStyleRule,
|
|
9
|
+
EditorStyleState,
|
|
10
|
+
EditorStyles,
|
|
11
|
+
EditorStyleTarget,
|
|
12
|
+
EditorStyleTargetKind,
|
|
13
|
+
} from '@astl/core';
|
|
14
|
+
import { CAPTION_TAG, COLUMN_TAG, COLUMNS_TAG, FIGURE_TAG } from '@astl/core';
|
|
15
|
+
import {
|
|
16
|
+
cleanClass,
|
|
17
|
+
cleanStyle,
|
|
18
|
+
isStyleName,
|
|
19
|
+
MAX_STYLE_RULES,
|
|
20
|
+
mergeStyles,
|
|
21
|
+
type StyleDeclaration,
|
|
22
|
+
styleText,
|
|
23
|
+
} from '@astl/core/declarations';
|
|
24
|
+
import { styleNameFrom } from '@astl/editor';
|
|
25
|
+
import type { Editor } from '@tiptap/core';
|
|
26
|
+
import { closeHistory } from '@tiptap/pm/history';
|
|
27
|
+
import type { Mark as PMMark, Node as PMNode } from '@tiptap/pm/model';
|
|
28
|
+
import { type EditorState, NodeSelection, TextSelection, type Transaction } from '@tiptap/pm/state';
|
|
29
|
+
import { LAYOUT, LAYOUT_COLUMN } from './blocks.js';
|
|
30
|
+
import { editLayoutIn, layoutAt, layoutState } from './layouts.js';
|
|
31
|
+
|
|
32
|
+
type Attributes = Record<string, string>;
|
|
33
|
+
|
|
34
|
+
/** The mark of `<span>`, the one that holds styles of text. */
|
|
35
|
+
const SPAN = 'textSpan';
|
|
36
|
+
const FIGURE = 'astlFigure';
|
|
37
|
+
const PICTURE = 'astlPicture';
|
|
38
|
+
|
|
39
|
+
/** The marks "clear formatting" takes off; links and a language stay, as do the words of a component. */
|
|
40
|
+
const CLEARED_MARKS: ReadonlySet<string> = new Set([
|
|
41
|
+
'bold',
|
|
42
|
+
'italic',
|
|
43
|
+
'underline',
|
|
44
|
+
'strike',
|
|
45
|
+
'code',
|
|
46
|
+
'sub',
|
|
47
|
+
'sup',
|
|
48
|
+
'mark',
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
/** A node's attributes that hold `style` and `class`: a record (`attributes`, or a figure's), or `null`: the node's own. */
|
|
52
|
+
type Holder = 'attributes' | 'picture' | 'caption' | null;
|
|
53
|
+
|
|
54
|
+
/** An element that takes a style, at `pos`. */
|
|
55
|
+
interface Place {
|
|
56
|
+
readonly kind: EditorStyleTargetKind;
|
|
57
|
+
readonly tag: string;
|
|
58
|
+
readonly pos: number;
|
|
59
|
+
readonly node: PMNode;
|
|
60
|
+
readonly holder: Holder;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const readHolder = (node: PMNode, holder: Holder): Attributes =>
|
|
64
|
+
holder === null
|
|
65
|
+
? Object.fromEntries(
|
|
66
|
+
['style', 'class'].flatMap((name) => (node.attrs[name] ? [[name, node.attrs[name] as string]] : [])),
|
|
67
|
+
)
|
|
68
|
+
: (node.attrs[holder] as Attributes);
|
|
69
|
+
|
|
70
|
+
/** Writes the holder's `style` and `class`: gone when empty, the other attributes as written. */
|
|
71
|
+
function writeHolder(tr: Transaction, place: Place, style: string, classes: string): void {
|
|
72
|
+
if (place.holder === null) {
|
|
73
|
+
tr.setNodeAttribute(place.pos, 'style', style === '' ? null : style);
|
|
74
|
+
tr.setNodeAttribute(place.pos, 'class', classes === '' ? null : classes);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const next = { ...(place.node.attrs[place.holder] as Attributes) };
|
|
78
|
+
if (style === '') delete next.style;
|
|
79
|
+
else next.style = style;
|
|
80
|
+
if (classes === '') delete next.class;
|
|
81
|
+
else next.class = classes;
|
|
82
|
+
tr.setNodeAttribute(place.pos, place.holder, next);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The declarations of `style` with `changes` made: what the allow-list keeps of the new ones, null ones gone. */
|
|
86
|
+
function changed(style: string, changes: EditorStyleChanges): readonly StyleDeclaration[] {
|
|
87
|
+
const adding = cleanStyle(
|
|
88
|
+
Object.entries(changes)
|
|
89
|
+
.flatMap(([name, value]) => (value === null ? [] : [`${name}: ${value}`]))
|
|
90
|
+
.join('; '),
|
|
91
|
+
).declarations;
|
|
92
|
+
const gone = new Set(Object.keys(changes));
|
|
93
|
+
return mergeStyles(
|
|
94
|
+
cleanStyle(style).declarations.filter(([name]) => !gone.has(name)),
|
|
95
|
+
adding,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Declarations on a placed box: a picture, a figure or a component. A figure's picture follows: a figure with a width
|
|
101
|
+
* has its picture fill it, or a small picture would stay small in a wide figure and the size would mean nothing to the eye.
|
|
102
|
+
*/
|
|
103
|
+
export function setPlacedStyle(
|
|
104
|
+
tr: Transaction,
|
|
105
|
+
pos: number,
|
|
106
|
+
node: PMNode,
|
|
107
|
+
changes: EditorStyleChanges,
|
|
108
|
+
): void {
|
|
109
|
+
const attrs = node.attrs.attributes as Attributes;
|
|
110
|
+
const place: Place = { kind: 'figure', tag: FIGURE_TAG, pos, node, holder: 'attributes' };
|
|
111
|
+
writeHolder(tr, place, styleText(changed(attrs.style ?? '', changes)), attrs.class ?? '');
|
|
112
|
+
if (node.type.name !== FIGURE || !Object.hasOwn(changes, 'width')) return;
|
|
113
|
+
const picture = node.attrs.picture as Attributes;
|
|
114
|
+
const fill = changes.width === null ? { width: null } : { width: '100%' };
|
|
115
|
+
writeHolder(
|
|
116
|
+
tr,
|
|
117
|
+
{ ...place, kind: 'picture', holder: 'picture' },
|
|
118
|
+
styleText(changed(picture.style ?? '', fill)),
|
|
119
|
+
picture.class ?? '',
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const firstClass = (classes: string | undefined): string | null =>
|
|
124
|
+
(classes ?? '').split(/\s+/).find((one) => one !== '') ?? null;
|
|
125
|
+
|
|
126
|
+
/** One text or inline atom in a range, with the part of it in the range. */
|
|
127
|
+
interface Run {
|
|
128
|
+
readonly from: number;
|
|
129
|
+
readonly to: number;
|
|
130
|
+
readonly spans: readonly PMMark[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Attribute values with `name` taken from every class list, deep: a child kept as data holds attributes too. */
|
|
134
|
+
function withoutClass(value: unknown, name: string): unknown {
|
|
135
|
+
if (Array.isArray(value)) return value.map((inner) => withoutClass(inner, name));
|
|
136
|
+
if (value === null || typeof value !== 'object') return value;
|
|
137
|
+
return Object.fromEntries(
|
|
138
|
+
Object.entries(value).flatMap(([key, inner]) => {
|
|
139
|
+
if (key !== 'class') return [[key, withoutClass(inner, name)]];
|
|
140
|
+
if (typeof inner !== 'string') return [[key, inner]];
|
|
141
|
+
const kept = inner
|
|
142
|
+
.split(/\s+/)
|
|
143
|
+
.filter((one) => one !== name && one !== '')
|
|
144
|
+
.join(' ');
|
|
145
|
+
return kept === '' ? [[key, null]] : [[key, kept]];
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Attributes without the keys `withoutClass` emptied: a record's `class` is left out, a mark's or a node's is null. */
|
|
151
|
+
const noEmptyClass = (attrs: unknown): unknown =>
|
|
152
|
+
Array.isArray(attrs)
|
|
153
|
+
? attrs.map(noEmptyClass)
|
|
154
|
+
: attrs === null || typeof attrs !== 'object'
|
|
155
|
+
? attrs
|
|
156
|
+
: Object.fromEntries(
|
|
157
|
+
Object.entries(attrs)
|
|
158
|
+
.filter(([key, inner]) => key !== 'class' || inner !== null)
|
|
159
|
+
.map(([key, inner]) => [key, noEmptyClass(inner)]),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
export function createStyles(
|
|
163
|
+
editor: Editor,
|
|
164
|
+
isComponent: (node: PMNode) => boolean,
|
|
165
|
+
tagOf: (node: PMNode) => string,
|
|
166
|
+
insert: (html: string) => void,
|
|
167
|
+
): EditorStyles {
|
|
168
|
+
const spanType = () => editor.schema.marks[SPAN];
|
|
169
|
+
|
|
170
|
+
/** A new undo step: a command never joins the typing before it. */
|
|
171
|
+
const step = (build: (tr: Transaction) => boolean): boolean => {
|
|
172
|
+
const tr = editor.state.tr;
|
|
173
|
+
closeHistory(tr);
|
|
174
|
+
if (!build(tr)) return false;
|
|
175
|
+
editor.view.dispatch(tr);
|
|
176
|
+
return true;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/** The text block `node` as a target, with what holds its style. */
|
|
180
|
+
const blockPlace = (node: PMNode, pos: number): Place => {
|
|
181
|
+
const figure = node.type.name === FIGURE;
|
|
182
|
+
const rich = node.type.name === 'paragraph' || node.type.name === 'heading';
|
|
183
|
+
return {
|
|
184
|
+
kind: 'block',
|
|
185
|
+
tag: figure
|
|
186
|
+
? CAPTION_TAG
|
|
187
|
+
: node.type.name === 'paragraph'
|
|
188
|
+
? 'p'
|
|
189
|
+
: node.type.name === 'heading'
|
|
190
|
+
? `h${node.attrs.level as number}`
|
|
191
|
+
: tagOf(node),
|
|
192
|
+
pos,
|
|
193
|
+
node,
|
|
194
|
+
holder: figure ? 'caption' : rich ? null : 'attributes',
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/** What the cursor is in, from the inside out. */
|
|
199
|
+
const places = (state: EditorState): Place[] => {
|
|
200
|
+
const { selection } = state;
|
|
201
|
+
const { $from } = selection;
|
|
202
|
+
const out: Place[] = [];
|
|
203
|
+
const figure = (node: PMNode, pos: number) => {
|
|
204
|
+
out.push(
|
|
205
|
+
{ kind: 'figure', tag: FIGURE_TAG, pos, node, holder: 'attributes' },
|
|
206
|
+
{ kind: 'picture', tag: 'img', pos, node, holder: 'picture' },
|
|
207
|
+
);
|
|
208
|
+
};
|
|
209
|
+
const selected = selection instanceof NodeSelection ? selection.node : null;
|
|
210
|
+
if (selected !== null) {
|
|
211
|
+
if (selected.type.name === PICTURE)
|
|
212
|
+
out.push({ kind: 'picture', tag: 'img', pos: selection.from, node: selected, holder: 'attributes' });
|
|
213
|
+
else if (selected.type.name === FIGURE) {
|
|
214
|
+
out.push(blockPlace(selected, selection.from));
|
|
215
|
+
figure(selected, selection.from);
|
|
216
|
+
} else if (isComponent(selected))
|
|
217
|
+
out.push({
|
|
218
|
+
kind: 'component',
|
|
219
|
+
tag: tagOf(selected),
|
|
220
|
+
pos: selection.from,
|
|
221
|
+
node: selected,
|
|
222
|
+
holder: 'attributes',
|
|
223
|
+
});
|
|
224
|
+
else if (selected.type.name === LAYOUT)
|
|
225
|
+
out.push({
|
|
226
|
+
kind: 'layout',
|
|
227
|
+
tag: COLUMNS_TAG,
|
|
228
|
+
pos: selection.from,
|
|
229
|
+
node: selected,
|
|
230
|
+
holder: 'attributes',
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
if (selected === null && $from.parent.isTextblock)
|
|
234
|
+
out.push(blockPlace($from.parent, $from.before($from.depth)));
|
|
235
|
+
// An atom next to the cursor counts as its component, as for `getSelection`.
|
|
236
|
+
if (selected === null) {
|
|
237
|
+
const before = $from.nodeBefore;
|
|
238
|
+
const after = $from.nodeAfter;
|
|
239
|
+
if (after !== null && isComponent(after))
|
|
240
|
+
out.push({ kind: 'component', tag: tagOf(after), pos: $from.pos, node: after, holder: 'attributes' });
|
|
241
|
+
else if (before !== null && isComponent(before))
|
|
242
|
+
out.push({
|
|
243
|
+
kind: 'component',
|
|
244
|
+
tag: tagOf(before),
|
|
245
|
+
pos: $from.pos - before.nodeSize,
|
|
246
|
+
node: before,
|
|
247
|
+
holder: 'attributes',
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
251
|
+
const node = $from.node(depth);
|
|
252
|
+
const pos = $from.before(depth);
|
|
253
|
+
if (node.type.name === FIGURE) figure(node, pos);
|
|
254
|
+
else if (isComponent(node))
|
|
255
|
+
out.push({ kind: 'component', tag: tagOf(node), pos, node, holder: 'attributes' });
|
|
256
|
+
else if (node.type.name === LAYOUT_COLUMN)
|
|
257
|
+
out.push({ kind: 'column', tag: COLUMN_TAG, pos, node, holder: 'attributes' });
|
|
258
|
+
else if (node.type.name === LAYOUT)
|
|
259
|
+
out.push({ kind: 'layout', tag: COLUMNS_TAG, pos, node, holder: 'attributes' });
|
|
260
|
+
}
|
|
261
|
+
return out;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/** The targets of a kind: every text block of the selection, or the one element around the cursor. */
|
|
265
|
+
const placesOf = (state: EditorState, kind: EditorStyleTargetKind): Place[] => {
|
|
266
|
+
const first = places(state).find((place) => place.kind === kind);
|
|
267
|
+
if (first === undefined) return [];
|
|
268
|
+
if (kind !== 'block') return [first];
|
|
269
|
+
const { from, to } = state.selection;
|
|
270
|
+
const blocks: Place[] = [];
|
|
271
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
272
|
+
if (node.isTextblock) blocks.push(blockPlace(node, pos));
|
|
273
|
+
return !node.isTextblock;
|
|
274
|
+
});
|
|
275
|
+
return blocks.length === 0 ? [first] : blocks;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
/** The text and inline atoms between two positions that can take a span. */
|
|
279
|
+
const runs = (doc: PMNode, from: number, to: number): Run[] => {
|
|
280
|
+
const type = spanType();
|
|
281
|
+
const out: Run[] = [];
|
|
282
|
+
if (type === undefined) return out;
|
|
283
|
+
doc.nodesBetween(from, to, (node, pos, parent) => {
|
|
284
|
+
if (!node.isInline || parent?.type.allowsMarkType(type) !== true) return true;
|
|
285
|
+
out.push({
|
|
286
|
+
from: Math.max(pos, from),
|
|
287
|
+
to: Math.min(pos + node.nodeSize, to),
|
|
288
|
+
spans: node.marks.filter((mark) => mark.type === type),
|
|
289
|
+
});
|
|
290
|
+
return false;
|
|
291
|
+
});
|
|
292
|
+
return out;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/** What the spans of a run come to as one: their attributes, the later over the earlier, their styles merged. */
|
|
296
|
+
const flat = (
|
|
297
|
+
spans: readonly PMMark[],
|
|
298
|
+
): { attrs: Record<string, string | null>; style: string; classes: string } => {
|
|
299
|
+
const attrs: Record<string, string | null> = {};
|
|
300
|
+
const lists: (readonly StyleDeclaration[])[] = [];
|
|
301
|
+
const classes: string[] = [];
|
|
302
|
+
for (const span of spans) {
|
|
303
|
+
for (const [name, value] of Object.entries(span.attrs)) {
|
|
304
|
+
if (name === 'style') lists.push(cleanStyle(String(value ?? '')).declarations);
|
|
305
|
+
else if (name === 'class') classes.push(String(value ?? ''));
|
|
306
|
+
else if (value !== null) attrs[name] = String(value);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return { attrs, style: styleText(mergeStyles(...lists)), classes: cleanClass(classes.join(' ')) };
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/** The mark of one span with these attributes, or null when it would hold nothing. */
|
|
313
|
+
const spanWith = (attrs: Record<string, string | null>, style: string, classes: string): PMMark | null => {
|
|
314
|
+
const empty = style === '' && classes === '' && Object.values(attrs).every((value) => value === null);
|
|
315
|
+
return empty
|
|
316
|
+
? null
|
|
317
|
+
: (spanType()?.create({
|
|
318
|
+
...attrs,
|
|
319
|
+
style: style === '' ? null : style,
|
|
320
|
+
class: classes === '' ? null : classes,
|
|
321
|
+
}) ?? null);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/** Makes each run of the selected text one span: `rewrite` says what it holds. */
|
|
325
|
+
const rewriteText = (
|
|
326
|
+
tr: Transaction,
|
|
327
|
+
rewrite: (style: string, classes: string) => { style: string; classes: string },
|
|
328
|
+
): boolean => {
|
|
329
|
+
const { from, to, empty } = tr.selection;
|
|
330
|
+
if (empty) {
|
|
331
|
+
// Only a cursor: what is typed next.
|
|
332
|
+
const $from = tr.selection.$from;
|
|
333
|
+
const marks = tr.storedMarks ?? editor.state.storedMarks ?? $from.marks();
|
|
334
|
+
const spans = marks.filter((mark) => mark.type === spanType());
|
|
335
|
+
const { attrs, style, classes } = flat(spans);
|
|
336
|
+
const next = rewrite(style, classes);
|
|
337
|
+
const span = spanWith(attrs, next.style, next.classes);
|
|
338
|
+
const others = marks.filter((mark) => mark.type !== spanType());
|
|
339
|
+
tr.setStoredMarks(span === null ? others : span.addToSet(others));
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
const type = spanType();
|
|
343
|
+
if (type === undefined) return false;
|
|
344
|
+
const before = tr.doc;
|
|
345
|
+
for (const run of runs(tr.doc, from, to)) {
|
|
346
|
+
const { attrs, style, classes } = flat(run.spans);
|
|
347
|
+
const next = rewrite(style, classes);
|
|
348
|
+
const span = spanWith(attrs, next.style, next.classes);
|
|
349
|
+
if (run.spans.length === 1 && span !== null && run.spans[0]?.eq(span)) continue;
|
|
350
|
+
tr.removeMark(run.from, run.to, type);
|
|
351
|
+
if (span !== null) tr.addMark(run.from, run.to, span);
|
|
352
|
+
}
|
|
353
|
+
return tr.doc !== before;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const textState = (
|
|
357
|
+
state: EditorState,
|
|
358
|
+
): Pick<EditorStyleState, 'text' | 'mixed' | 'textClass' | 'textEnabled' | 'hasSelection'> => {
|
|
359
|
+
const { selection } = state;
|
|
360
|
+
const type = spanType();
|
|
361
|
+
const parentTakes = selection.$from.parent.inlineContent && type !== undefined;
|
|
362
|
+
const textEnabled =
|
|
363
|
+
!(selection instanceof NodeSelection) &&
|
|
364
|
+
parentTakes &&
|
|
365
|
+
selection.$from.parent.type.allowsMarkType(type as NonNullable<typeof type>);
|
|
366
|
+
const list = selection.empty
|
|
367
|
+
? [(state.storedMarks ?? selection.$from.marks()).filter((mark) => mark.type === type)]
|
|
368
|
+
: runs(state.doc, selection.from, selection.to).map((run) => run.spans);
|
|
369
|
+
const seen = list.map(flat);
|
|
370
|
+
const text: Record<string, string> = {};
|
|
371
|
+
const mixed = new Set<string>();
|
|
372
|
+
const all = seen.map(({ style }) => new Map(cleanStyle(style).declarations));
|
|
373
|
+
for (const name of new Set(all.flatMap((one) => [...one.keys()]))) {
|
|
374
|
+
const values = new Set(all.map((one) => one.get(name)));
|
|
375
|
+
if (values.size === 1) text[name] = [...values][0] as string;
|
|
376
|
+
else mixed.add(name);
|
|
377
|
+
}
|
|
378
|
+
const classes = new Set(seen.map(({ classes: found }) => found));
|
|
379
|
+
return {
|
|
380
|
+
textEnabled,
|
|
381
|
+
hasSelection: !selection.empty && !(selection instanceof NodeSelection),
|
|
382
|
+
text,
|
|
383
|
+
mixed: [...mixed],
|
|
384
|
+
textClass: classes.size === 1 ? firstClass([...classes][0]) : null,
|
|
385
|
+
};
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const toTarget = (place: Place): EditorStyleTarget => {
|
|
389
|
+
const held = readHolder(place.node, place.holder);
|
|
390
|
+
return {
|
|
391
|
+
kind: place.kind,
|
|
392
|
+
tag: place.tag,
|
|
393
|
+
style: cleanStyle(held.style ?? '').style,
|
|
394
|
+
className: firstClass(held.class),
|
|
395
|
+
};
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const rulesOf = (state: EditorState) => state.doc.attrs.rules as readonly Attributes[];
|
|
399
|
+
const toRule = ({ name, label, for: target, style }: Attributes): EditorStyleRule => ({
|
|
400
|
+
...(name === undefined ? {} : { name }),
|
|
401
|
+
...(label === undefined ? {} : { label }),
|
|
402
|
+
...(target === undefined ? {} : { for: target }),
|
|
403
|
+
style: cleanStyle(style ?? '').style,
|
|
404
|
+
});
|
|
405
|
+
const sameRule = (a: Pick<EditorStyleRule, 'name' | 'for'>, b: Pick<EditorStyleRule, 'name' | 'for'>) =>
|
|
406
|
+
(a.name !== undefined && a.name === b.name) || (a.for !== undefined && a.for === b.for);
|
|
407
|
+
|
|
408
|
+
/** Rewrites the style and class of each target of a kind in `tr`; returns whether the document changed. */
|
|
409
|
+
const targetsInto = (
|
|
410
|
+
tr: Transaction,
|
|
411
|
+
kind: EditorStyleTargetKind,
|
|
412
|
+
rewrite: (style: string, classes: string) => { style: string; classes: string },
|
|
413
|
+
): boolean => {
|
|
414
|
+
for (const place of placesOf(editor.state, kind)) {
|
|
415
|
+
const held = readHolder(place.node, place.holder);
|
|
416
|
+
const now = cleanStyle(held.style ?? '').style;
|
|
417
|
+
const classes = held.class ?? '';
|
|
418
|
+
const next = rewrite(now, classes);
|
|
419
|
+
if (next.style !== now || next.classes !== classes) writeHolder(tr, place, next.style, next.classes);
|
|
420
|
+
}
|
|
421
|
+
return tr.docChanged;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const writeTargets = (
|
|
425
|
+
kind: EditorStyleTargetKind,
|
|
426
|
+
rewrite: (style: string, classes: string) => { style: string; classes: string },
|
|
427
|
+
): boolean => step((tr) => targetsInto(tr, kind, rewrite));
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Adds a rule in `tr`, or changes the one with its `name` or `for` in place; a tag's rule without declarations goes.
|
|
431
|
+
* False, writing nothing, when nothing would change or a new rule is past the limit.
|
|
432
|
+
*/
|
|
433
|
+
const ruleInto = (tr: Transaction, rule: EditorStyleRule): boolean => {
|
|
434
|
+
const rules = tr.doc.attrs.rules as readonly Attributes[];
|
|
435
|
+
const style = cleanStyle(rule.style).style;
|
|
436
|
+
const at = rules.findIndex((one) => sameRule(toRule(one), rule));
|
|
437
|
+
const gone = rule.for !== undefined && style === '';
|
|
438
|
+
if (at < 0 && (gone || rules.length >= MAX_STYLE_RULES)) return false;
|
|
439
|
+
const record: Attributes = {
|
|
440
|
+
...(rule.name === undefined ? {} : { name: rule.name }),
|
|
441
|
+
...(rule.label === undefined ? {} : { label: rule.label }),
|
|
442
|
+
...(rule.for === undefined ? {} : { for: rule.for }),
|
|
443
|
+
...(style === '' ? {} : { style }),
|
|
444
|
+
};
|
|
445
|
+
const next = gone
|
|
446
|
+
? rules.filter((_, i) => i !== at)
|
|
447
|
+
: at < 0
|
|
448
|
+
? [...rules, record]
|
|
449
|
+
: rules.map((one, i) => (i === at ? record : one));
|
|
450
|
+
if (JSON.stringify(next) === JSON.stringify(rules)) return false;
|
|
451
|
+
tr.setDocAttribute('rules', next);
|
|
452
|
+
return true;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
return {
|
|
456
|
+
state(): EditorStyleState {
|
|
457
|
+
// A kit element may draw once more after its surface went away.
|
|
458
|
+
if (editor.isDestroyed)
|
|
459
|
+
return { textEnabled: false, hasSelection: false, text: {}, mixed: [], textClass: null, targets: [] };
|
|
460
|
+
const state = editor.state;
|
|
461
|
+
return { ...textState(state), targets: places(state).map(toTarget) };
|
|
462
|
+
},
|
|
463
|
+
setText(changes) {
|
|
464
|
+
return step((tr) =>
|
|
465
|
+
rewriteText(tr, (style, classes) => ({ style: styleText(changed(style, changes)), classes })),
|
|
466
|
+
);
|
|
467
|
+
},
|
|
468
|
+
setTarget(kind, changes) {
|
|
469
|
+
if (kind === 'figure') {
|
|
470
|
+
return step((tr) => {
|
|
471
|
+
const place = places(editor.state).find((one) => one.kind === 'figure');
|
|
472
|
+
if (place === undefined) return false;
|
|
473
|
+
setPlacedStyle(tr, place.pos, place.node, changes);
|
|
474
|
+
return tr.docChanged;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
return writeTargets(kind, (style, classes) => ({ style: styleText(changed(style, changes)), classes }));
|
|
478
|
+
},
|
|
479
|
+
replaceTarget(kind, style) {
|
|
480
|
+
return writeTargets(kind, (_, classes) => ({ style: cleanStyle(style).style, classes }));
|
|
481
|
+
},
|
|
482
|
+
setClass(where, name, rule) {
|
|
483
|
+
if (name !== null && !isStyleName(name)) return false;
|
|
484
|
+
if (rule !== undefined && (rule.name !== name || rule.for !== undefined)) return false;
|
|
485
|
+
const rewrite = (style: string) => ({ style, classes: name ?? '' });
|
|
486
|
+
return step((tr) => {
|
|
487
|
+
if (!(where === 'text' ? rewriteText(tr, rewrite) : targetsInto(tr, where, rewrite))) return false;
|
|
488
|
+
// A ready style's rule rides in the same step, so one undo takes both; the document's own rule of the name wins.
|
|
489
|
+
return (
|
|
490
|
+
rule === undefined || rulesOf(editor.state).some((one) => one.name === name) || ruleInto(tr, rule)
|
|
491
|
+
);
|
|
492
|
+
});
|
|
493
|
+
},
|
|
494
|
+
clear() {
|
|
495
|
+
return step((tr) => {
|
|
496
|
+
const { from, to, empty } = tr.selection;
|
|
497
|
+
if (tr.selection instanceof NodeSelection) return false;
|
|
498
|
+
if (empty) tr.setStoredMarks([]);
|
|
499
|
+
else {
|
|
500
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
501
|
+
if (!node.isInline) return true;
|
|
502
|
+
for (const mark of node.marks) {
|
|
503
|
+
if (!CLEARED_MARKS.has(mark.type.name)) continue;
|
|
504
|
+
tr.removeMark(Math.max(pos, from), Math.min(pos + node.nodeSize, to), mark);
|
|
505
|
+
}
|
|
506
|
+
return false;
|
|
507
|
+
});
|
|
508
|
+
rewriteText(tr, () => ({ style: '', classes: '' }));
|
|
509
|
+
}
|
|
510
|
+
for (const place of blocksCovered(editor.state)) {
|
|
511
|
+
const held = readHolder(place.node, place.holder);
|
|
512
|
+
if (held.style !== undefined || held.class !== undefined) writeHolder(tr, place, '', '');
|
|
513
|
+
}
|
|
514
|
+
return tr.docChanged || empty;
|
|
515
|
+
});
|
|
516
|
+
},
|
|
517
|
+
rules: () => (editor.isDestroyed ? [] : rulesOf(editor.state).map(toRule)),
|
|
518
|
+
setRule(rule) {
|
|
519
|
+
if (rule.name !== undefined && !isStyleName(rule.name)) return false;
|
|
520
|
+
return step((tr) => ruleInto(tr, rule));
|
|
521
|
+
},
|
|
522
|
+
removeRule(rule) {
|
|
523
|
+
return step((tr) => {
|
|
524
|
+
const rules = rulesOf(editor.state);
|
|
525
|
+
const next = rules.filter((one) => !sameRule(toRule(one), rule));
|
|
526
|
+
if (next.length === rules.length) return false;
|
|
527
|
+
tr.setDocAttribute('rules', next);
|
|
528
|
+
const { name } = rule;
|
|
529
|
+
if (name === undefined) return true;
|
|
530
|
+
const docAttrs = noEmptyClass(withoutClass(tr.doc.attrs.attributes, name));
|
|
531
|
+
tr.setDocAttribute('attributes', docAttrs);
|
|
532
|
+
// No class of a style that is gone stays behind: it would be a warning, and a surprise if the name came back.
|
|
533
|
+
editor.state.doc.descendants((node, pos) => {
|
|
534
|
+
for (const [key, value] of Object.entries(node.attrs)) {
|
|
535
|
+
const kept = key === 'class' ? null : withoutClass(value, name);
|
|
536
|
+
if (key === 'class' && typeof value === 'string' && value.split(/\s+/).includes(name)) {
|
|
537
|
+
const others = value.split(/\s+/).filter((one) => one !== name && one !== '');
|
|
538
|
+
tr.setNodeAttribute(pos, key, others.length === 0 ? null : others.join(' '));
|
|
539
|
+
} else if (key !== 'class' && JSON.stringify(kept) !== JSON.stringify(value))
|
|
540
|
+
tr.setNodeAttribute(pos, key, noEmptyClass(kept));
|
|
541
|
+
}
|
|
542
|
+
for (const mark of node.marks) {
|
|
543
|
+
const classes = String(mark.attrs.class ?? '').split(/\s+/);
|
|
544
|
+
if (!classes.includes(name)) continue;
|
|
545
|
+
const others = classes.filter((one) => one !== name && one !== '').join(' ');
|
|
546
|
+
const attrs = { ...mark.attrs, class: others === '' ? null : others };
|
|
547
|
+
const end = pos + node.nodeSize;
|
|
548
|
+
tr.removeMark(pos, end, mark);
|
|
549
|
+
const empty = mark.type === spanType() && Object.values(attrs).every((value) => value === null);
|
|
550
|
+
if (!empty) tr.addMark(pos, end, mark.type.create(attrs));
|
|
551
|
+
}
|
|
552
|
+
return true;
|
|
553
|
+
});
|
|
554
|
+
return true;
|
|
555
|
+
});
|
|
556
|
+
},
|
|
557
|
+
saveRule(kind, label) {
|
|
558
|
+
const rules = rulesOf(editor.state);
|
|
559
|
+
const place = places(editor.state).find((one) => one.kind === kind);
|
|
560
|
+
if (place === undefined || rules.length >= MAX_STYLE_RULES) return null;
|
|
561
|
+
const held = readHolder(place.node, place.holder);
|
|
562
|
+
const name = styleNameFrom(label, new Set(rules.flatMap((rule) => rule.name ?? [])));
|
|
563
|
+
const style = cleanStyle(held.style ?? '').style;
|
|
564
|
+
step((tr) => {
|
|
565
|
+
tr.setDocAttribute('rules', [...rules, { name, label, ...(style === '' ? {} : { style }) }]);
|
|
566
|
+
writeHolder(tr, place, '', name);
|
|
567
|
+
return true;
|
|
568
|
+
});
|
|
569
|
+
return name;
|
|
570
|
+
},
|
|
571
|
+
insertLayout(html) {
|
|
572
|
+
const before = editor.state.doc;
|
|
573
|
+
closeHistoryNow();
|
|
574
|
+
insert(html);
|
|
575
|
+
if (editor.state.doc === before) return false;
|
|
576
|
+
// The cursor into the first empty block of the new layout (the text column, beside a picture).
|
|
577
|
+
const { $from } = editor.state.selection;
|
|
578
|
+
let at: number | null = null;
|
|
579
|
+
for (let depth = $from.depth; at === null && depth > 0; depth--) {
|
|
580
|
+
if ($from.node(depth).type.name === LAYOUT) at = $from.before(depth);
|
|
581
|
+
}
|
|
582
|
+
if (at === null) {
|
|
583
|
+
// After a layout at the end of its page, the selection is on what follows it.
|
|
584
|
+
at = $from.nodeBefore?.type.name === LAYOUT ? $from.pos - $from.nodeBefore.nodeSize : null;
|
|
585
|
+
}
|
|
586
|
+
const layout = at === null ? null : editor.state.doc.nodeAt(at);
|
|
587
|
+
if (at === null || layout === null) {
|
|
588
|
+
closeHistoryNow();
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
const start = at;
|
|
592
|
+
let target: number | null = null;
|
|
593
|
+
layout.descendants((node, pos) => {
|
|
594
|
+
if (target === null && node.isTextblock && node.content.size === 0) target = start + 1 + pos + 1;
|
|
595
|
+
return target === null;
|
|
596
|
+
});
|
|
597
|
+
const tr = editor.state.tr;
|
|
598
|
+
if (target !== null) tr.setSelection(TextSelection.create(tr.doc, target));
|
|
599
|
+
// A layout that ends its page leaves nowhere to go on writing: a line after it, in the same step.
|
|
600
|
+
const end = start + layout.nodeSize;
|
|
601
|
+
const paragraph = editor.schema.nodes.paragraph;
|
|
602
|
+
if (tr.doc.resolve(end).nodeAfter === null && paragraph !== undefined)
|
|
603
|
+
tr.insert(end, paragraph.create());
|
|
604
|
+
editor.view.dispatch(tr);
|
|
605
|
+
// What is written in it next is an undo step of its own, however fast it follows.
|
|
606
|
+
closeHistoryNow();
|
|
607
|
+
return true;
|
|
608
|
+
},
|
|
609
|
+
layout() {
|
|
610
|
+
if (editor.isDestroyed) return null;
|
|
611
|
+
const at = layoutAt(editor.state);
|
|
612
|
+
return at === null ? null : layoutState(at.node);
|
|
613
|
+
},
|
|
614
|
+
editLayout(edit) {
|
|
615
|
+
const at = layoutAt(editor.state);
|
|
616
|
+
if (at === null) return false;
|
|
617
|
+
const done = step((tr) => editLayoutIn(tr, at, edit));
|
|
618
|
+
if (done) editor.view.focus();
|
|
619
|
+
return done;
|
|
620
|
+
},
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
/** Starts a new undo step for a command that goes through `insert`. */
|
|
624
|
+
function closeHistoryNow(): void {
|
|
625
|
+
editor.view.dispatch(closeHistory(editor.state.tr));
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/** The blocks the selection covers whole; with only a cursor, its block. */
|
|
629
|
+
function blocksCovered(state: EditorState): Place[] {
|
|
630
|
+
const { from, to, empty } = state.selection;
|
|
631
|
+
return placesOf(state, 'block').filter((place) => {
|
|
632
|
+
if (empty) return true;
|
|
633
|
+
const start = place.pos + 1;
|
|
634
|
+
const end = place.pos + 1 + place.node.content.size;
|
|
635
|
+
return from <= start && to >= end;
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
}
|