@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/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@astl/editor-tiptap`: `<astl-editor>`, the authoring kit around a TipTap surface. This entry never imports
|
|
3
|
+
* TipTap: the surface is a dynamic import, made only when no other editor is plugged in. Hosts that build their own
|
|
4
|
+
* TipTap editor take `astrolabeExtensions` and `createTiptapAdapter` from `@astl/editor-tiptap/tiptap`.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
export type { DefineEditorOptions, EditorHostView } from './define.js';
|
|
9
|
+
export { defineEditor } from './define.js';
|
|
10
|
+
export { AstlEditor } from './element.js';
|
package/src/layouts.ts
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commands on a layout once it is in the document: unwrap it (its columns' content stays in place, one column after
|
|
3
|
+
* another), delete it with its content, or give it another shape (`EditorStyles.editLayout`). Each writes into a
|
|
4
|
+
* transaction, so a caller makes it one undo step; the cursor stays in what it was in, or goes where the layout was.
|
|
5
|
+
* And the mark on the layout with the cursor, which brings those commands in a host's card.
|
|
6
|
+
*/
|
|
7
|
+
import type { EditorLayoutEdit, EditorLayoutShape, EditorLayoutState } from '@astl/core';
|
|
8
|
+
import { cleanStyle, styleText } from '@astl/core/declarations';
|
|
9
|
+
import { LAYOUT_COLUMNS, LAYOUT_SHAPES } from '@astl/editor';
|
|
10
|
+
import { Extension } from '@tiptap/core';
|
|
11
|
+
import { Fragment, type Node as PMNode } from '@tiptap/pm/model';
|
|
12
|
+
import {
|
|
13
|
+
type EditorState,
|
|
14
|
+
NodeSelection,
|
|
15
|
+
Plugin,
|
|
16
|
+
Selection,
|
|
17
|
+
TextSelection,
|
|
18
|
+
type Transaction,
|
|
19
|
+
} from '@tiptap/pm/state';
|
|
20
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
21
|
+
import { LAYOUT, LAYOUT_COLUMN } from './blocks.js';
|
|
22
|
+
|
|
23
|
+
const FIGURE = 'astlFigure';
|
|
24
|
+
|
|
25
|
+
type Attributes = Record<string, string>;
|
|
26
|
+
|
|
27
|
+
/** A layout in the document: where it starts, and its node. */
|
|
28
|
+
export interface LayoutAt {
|
|
29
|
+
readonly pos: number;
|
|
30
|
+
readonly node: PMNode;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The layout the selection starts in, or null. */
|
|
34
|
+
export function layoutAt(state: Pick<EditorState, 'selection'>): LayoutAt | null {
|
|
35
|
+
const { $from } = state.selection;
|
|
36
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
37
|
+
const node = $from.node(depth);
|
|
38
|
+
if (node.type.name === LAYOUT) return { pos: $from.before(depth), node };
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const growOf = (column: PMNode): number | null => {
|
|
44
|
+
const style = (column.attrs.attributes as Attributes).style ?? '';
|
|
45
|
+
const grow = cleanStyle(style).declarations.find(([name]) => name === 'flex-grow')?.[1];
|
|
46
|
+
const value = grow === undefined ? Number.NaN : Number(grow);
|
|
47
|
+
// An equal share is the default `flex: 1`: a grow of 1 is the same.
|
|
48
|
+
return Number.isNaN(value) || value === 1 ? null : value;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Whether a layout holds nothing but empty paragraphs. */
|
|
52
|
+
export const isEmptyLayout = (layout: PMNode): boolean => {
|
|
53
|
+
let empty = true;
|
|
54
|
+
layout.descendants((node) => {
|
|
55
|
+
if (!empty) return false;
|
|
56
|
+
if (node.type.name === LAYOUT_COLUMN) return true;
|
|
57
|
+
if (node.type.name !== 'paragraph' || node.content.size > 0) empty = false;
|
|
58
|
+
return false;
|
|
59
|
+
});
|
|
60
|
+
return empty;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The shape whose columns a layout has, or null. */
|
|
64
|
+
export function shapeOf(layout: PMNode): EditorLayoutShape | null {
|
|
65
|
+
const grows = layout.children.map(growOf);
|
|
66
|
+
return (
|
|
67
|
+
LAYOUT_SHAPES.find((shape) => {
|
|
68
|
+
const wanted = LAYOUT_COLUMNS[shape];
|
|
69
|
+
return wanted.length === grows.length && wanted.every((grow, i) => grow === grows[i]);
|
|
70
|
+
}) ?? null
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const layoutState = (layout: PMNode): EditorLayoutState => ({
|
|
75
|
+
shape: shapeOf(layout),
|
|
76
|
+
columns: layout.childCount,
|
|
77
|
+
empty: isEmptyLayout(layout),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/** The cursor at the first place for text from `pos` on (`dir` 1), or back from it (-1), or the nearest one. */
|
|
81
|
+
const cursorFrom = (tr: Transaction, pos: number, dir: 1 | -1 = 1) => {
|
|
82
|
+
const $pos = tr.doc.resolve(Math.min(pos, tr.doc.content.size));
|
|
83
|
+
tr.setSelection(Selection.findFrom($pos, dir, true) ?? Selection.near($pos, dir));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Replaces the layout with its columns' content, in place, one column after another. */
|
|
87
|
+
export function unwrapLayoutIn(tr: Transaction, at: LayoutAt): boolean {
|
|
88
|
+
const content = at.node.children.flatMap((column) => column.children);
|
|
89
|
+
tr.replaceWith(at.pos, at.pos + at.node.nodeSize, Fragment.from(content));
|
|
90
|
+
cursorFrom(tr, at.pos);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Deletes the layout with its content; on a page with nothing else, an empty paragraph takes its place. The cursor goes
|
|
96
|
+
* to what follows, or with `back` (Backspace) to the end of what came before.
|
|
97
|
+
*/
|
|
98
|
+
export function deleteLayoutIn(tr: Transaction, at: LayoutAt, back = false): boolean {
|
|
99
|
+
const end = at.pos + at.node.nodeSize;
|
|
100
|
+
const parent = tr.doc.resolve(at.pos).parent;
|
|
101
|
+
const paragraph = tr.doc.type.schema.nodes.paragraph;
|
|
102
|
+
if (parent.childCount === 1 && paragraph !== undefined) tr.replaceWith(at.pos, end, paragraph.create());
|
|
103
|
+
else tr.delete(at.pos, end);
|
|
104
|
+
cursorFrom(tr, at.pos, back ? -1 : 1);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The column's attributes with the shape's `flex-grow` in the place of its own. */
|
|
109
|
+
function withGrow(attributes: Attributes, grow: number | null): Attributes {
|
|
110
|
+
const kept = cleanStyle(attributes.style ?? '').declarations.filter(([name]) => name !== 'flex-grow');
|
|
111
|
+
const style = styleText(grow === null ? kept : [...kept, ['flex-grow', String(grow)]]);
|
|
112
|
+
const rest = Object.fromEntries(Object.entries(attributes).filter(([name]) => name !== 'style'));
|
|
113
|
+
return style === '' ? rest : { ...rest, style };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Gives the layout `shape`'s columns: each column keeps its content and its own style but its width; one that goes
|
|
118
|
+
* gives its content to the last one left, a new one starts with an empty paragraph. The cursor keeps its place in the
|
|
119
|
+
* text. False for the shape the layout has.
|
|
120
|
+
*/
|
|
121
|
+
export function reshapeLayoutIn(tr: Transaction, at: LayoutAt, shape: EditorLayoutShape): boolean {
|
|
122
|
+
const { node, pos } = at;
|
|
123
|
+
if (shapeOf(node) === shape) return false;
|
|
124
|
+
const grows = LAYOUT_COLUMNS[shape];
|
|
125
|
+
const old = node.children;
|
|
126
|
+
const schema = tr.doc.type.schema;
|
|
127
|
+
const columnType = schema.nodes[LAYOUT_COLUMN];
|
|
128
|
+
const paragraph = schema.nodes.paragraph;
|
|
129
|
+
if (columnType === undefined || paragraph === undefined) return false;
|
|
130
|
+
// Where the cursor is: which column, and how far into its content.
|
|
131
|
+
const { from } = tr.selection;
|
|
132
|
+
let cursor: { column: number; offset: number } | null = null;
|
|
133
|
+
node.forEach((column, offset, index) => {
|
|
134
|
+
const start = pos + 1 + offset + 1;
|
|
135
|
+
if (from >= start && from <= start + column.content.size)
|
|
136
|
+
cursor = { column: index, offset: from - start };
|
|
137
|
+
});
|
|
138
|
+
const last = grows.length - 1;
|
|
139
|
+
const columns = grows.map((grow, i) => {
|
|
140
|
+
const own = old[i];
|
|
141
|
+
let content = own === undefined ? Fragment.from(paragraph.create()) : own.content;
|
|
142
|
+
// What would be lost with the columns past the last: their content joins it, in order.
|
|
143
|
+
if (i === last) for (const gone of old.slice(i + 1)) content = content.append(gone.content);
|
|
144
|
+
return columnType.create(
|
|
145
|
+
{ attributes: withGrow((own?.attrs.attributes as Attributes) ?? {}, grow) },
|
|
146
|
+
content,
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
tr.replaceWith(pos, pos + node.nodeSize, node.type.create(node.attrs, columns));
|
|
150
|
+
const kept = cursor as { column: number; offset: number } | null;
|
|
151
|
+
if (kept !== null) {
|
|
152
|
+
const index = Math.min(kept.column, last);
|
|
153
|
+
let offset = kept.offset;
|
|
154
|
+
for (let i = index; i < kept.column; i++) offset += old[i]?.content.size ?? 0;
|
|
155
|
+
let start = pos + 1;
|
|
156
|
+
for (let i = 0; i < index; i++) start += columns[i]?.nodeSize ?? 0;
|
|
157
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(start + 1 + offset)));
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Runs an edit on the layout at `pos`, one undo step, and puts the focus back in the text. */
|
|
163
|
+
export function editLayoutIn(tr: Transaction, at: LayoutAt, edit: EditorLayoutEdit): boolean {
|
|
164
|
+
if (edit === 'unwrap') return unwrapLayoutIn(tr, at);
|
|
165
|
+
if (edit === 'delete') return deleteLayoutIn(tr, at);
|
|
166
|
+
return reshapeLayoutIn(tr, at, edit);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The layout whose own settings the selection brings, as the style targets list it (`EditorStyles.state`): first when
|
|
171
|
+
* the layout or one of its columns is selected whole, or past the text blocks and the column with the cursor in its
|
|
172
|
+
* text, when nothing of its own is between: no component or figure there, no inline component next to the cursor.
|
|
173
|
+
* Null otherwise (a block selected in a column lists its column first), and with no layout.
|
|
174
|
+
*/
|
|
175
|
+
export function currentLayoutAt(
|
|
176
|
+
state: Pick<EditorState, 'selection'>,
|
|
177
|
+
isComponent: (node: PMNode) => boolean,
|
|
178
|
+
): LayoutAt | null {
|
|
179
|
+
const { selection } = state;
|
|
180
|
+
const { $from } = selection;
|
|
181
|
+
if (selection instanceof NodeSelection) {
|
|
182
|
+
if (selection.node.type.name === LAYOUT) return { pos: selection.from, node: selection.node };
|
|
183
|
+
return $from.parent.type.name === LAYOUT ? { pos: $from.before($from.depth), node: $from.parent } : null;
|
|
184
|
+
}
|
|
185
|
+
if (!$from.parent.isTextblock) return null;
|
|
186
|
+
for (const near of [$from.nodeBefore, $from.nodeAfter]) if (near !== null && isComponent(near)) return null;
|
|
187
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
188
|
+
const node = $from.node(depth);
|
|
189
|
+
if (node.type.name === LAYOUT) return { pos: $from.before(depth), node };
|
|
190
|
+
if (node.type.name === FIGURE || isComponent(node)) return null;
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The layout with the cursor (`currentLayoutAt`), marked on the surface without a row of its own, so the page keeps
|
|
197
|
+
* the place the student sees: the class `astl-layout-current` (its columns' edges in the accent colour, as wide as
|
|
198
|
+
* before) and `data-astl-current`, how a host finds it in the light DOM, as it finds the frame with the cursor. Its
|
|
199
|
+
* commands are in its style form.
|
|
200
|
+
*/
|
|
201
|
+
export function currentLayoutExtension(isComponent: (node: PMNode) => boolean): Extension {
|
|
202
|
+
return Extension.create({
|
|
203
|
+
name: 'astlCurrentLayout',
|
|
204
|
+
addProseMirrorPlugins: () => [
|
|
205
|
+
new Plugin({
|
|
206
|
+
props: {
|
|
207
|
+
decorations(state) {
|
|
208
|
+
const at = currentLayoutAt(state, isComponent);
|
|
209
|
+
if (at === null) return DecorationSet.empty;
|
|
210
|
+
const mark = Decoration.node(at.pos, at.pos + at.node.nodeSize, {
|
|
211
|
+
class: 'astl-layout-current',
|
|
212
|
+
'data-astl-current': '',
|
|
213
|
+
});
|
|
214
|
+
return DecorationSet.create(state.doc, [mark]);
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
],
|
|
219
|
+
});
|
|
220
|
+
}
|