@atmo-dev/kit 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/articles/ArticleCard.svelte +97 -0
- package/dist/articles/ArticleCard.svelte.d.ts +20 -0
- package/dist/articles/ArticleCodeBlock.svelte +121 -0
- package/dist/articles/ArticleCodeBlock.svelte.d.ts +11 -0
- package/dist/articles/ArticleEditor.svelte +340 -0
- package/dist/articles/ArticleEditor.svelte.d.ts +48 -0
- package/dist/articles/ArticleLinkEmbed.svelte +50 -0
- package/dist/articles/ArticleLinkEmbed.svelte.d.ts +5 -0
- package/dist/articles/ArticleView.svelte +128 -0
- package/dist/articles/ArticleView.svelte.d.ts +46 -0
- package/dist/articles/PublicationIcon.svelte +37 -0
- package/dist/articles/PublicationIcon.svelte.d.ts +10 -0
- package/dist/articles/article-link-embed.d.ts +36 -0
- package/dist/articles/article-link-embed.js +29 -0
- package/dist/articles/dates.d.ts +2 -0
- package/dist/articles/dates.js +12 -0
- package/dist/articles/images.d.ts +5 -0
- package/dist/articles/images.js +44 -0
- package/dist/articles/markdown.d.ts +15 -0
- package/dist/articles/markdown.js +52 -0
- package/dist/articles/paste.d.ts +11 -0
- package/dist/articles/paste.js +27 -0
- package/dist/articles/records.d.ts +166 -0
- package/dist/articles/records.js +423 -0
- package/dist/articles/styles.d.ts +4 -0
- package/dist/articles/styles.js +14 -0
- package/dist/articles-data.d.ts +8 -0
- package/dist/articles-data.js +8 -0
- package/dist/articles.d.ts +10 -0
- package/dist/articles.js +8 -0
- package/dist/components/ExternalEmbed.svelte +16 -2
- package/dist/components/ExternalEmbed.svelte.d.ts +6 -0
- package/dist/components/IconMaker.svelte +119 -0
- package/dist/components/IconMaker.svelte.d.ts +9 -0
- package/dist/components/IconRail.svelte +9 -6
- package/dist/components/IconRail.svelte.d.ts +2 -0
- package/dist/components/LinkEmbed.svelte +24 -21
- package/dist/components/LinkEmbed.svelte.d.ts +3 -2
- package/dist/components/PaletteApply.svelte +62 -0
- package/dist/components/PaletteApply.svelte.d.ts +9 -0
- package/dist/components/PalettePicker.svelte +92 -0
- package/dist/components/PalettePicker.svelte.d.ts +13 -0
- package/dist/components/PostComments.svelte +129 -0
- package/dist/components/PostComments.svelte.d.ts +15 -0
- package/dist/components/RichText.svelte +199 -9
- package/dist/components/RichText.svelte.d.ts +17 -0
- package/dist/components/RichTextEditor.svelte +155 -13
- package/dist/components/RichTextEditor.svelte.d.ts +26 -3
- package/dist/components/composer/ComposerPost.svelte +20 -4
- package/dist/components/composer/ComposerPost.svelte.d.ts +3 -1
- package/dist/components/composer/PostComposer.svelte +29 -3
- package/dist/components/composer/PostComposer.svelte.d.ts +12 -1
- package/dist/editor/EditorMenus.svelte +438 -0
- package/dist/editor/EditorMenus.svelte.d.ts +9 -0
- package/dist/editor/SlashOptions.svelte +45 -0
- package/dist/editor/SlashOptions.svelte.d.ts +12 -0
- package/dist/editor/article-commands.d.ts +44 -0
- package/dist/editor/article-commands.js +289 -0
- package/dist/editor/article-extensions.d.ts +8 -0
- package/dist/editor/article-extensions.js +315 -0
- package/dist/editor/article-menus.d.ts +2 -0
- package/dist/editor/article-menus.js +3 -0
- package/dist/editor/document.d.ts +5 -0
- package/dist/editor/document.js +163 -12
- package/dist/editor/image-node.d.ts +8 -0
- package/dist/editor/image-node.js +75 -0
- package/dist/editor/markdown-extensions.d.ts +1 -5
- package/dist/editor/markdown-extensions.js +37 -2
- package/dist/editor/slash-menu.svelte.d.ts +9 -0
- package/dist/editor/slash-menu.svelte.js +134 -0
- package/dist/embeds/PlayerLinkEmbed.svelte +9 -1
- package/dist/embeds/PostLinkEmbed.svelte +12 -29
- package/dist/embeds/PostLinkEmbed.svelte.d.ts +3 -0
- package/dist/embeds/ProfileLinkEmbed.svelte +12 -17
- package/dist/embeds/ProfileLinkEmbed.svelte.d.ts +3 -0
- package/dist/embeds/defaults.d.ts +4 -2
- package/dist/embeds/defaults.js +7 -3
- package/dist/embeds/load.d.ts +44 -0
- package/dist/embeds/load.js +139 -0
- package/dist/embeds/types.d.ts +18 -3
- package/dist/events/EventComments.svelte +4 -101
- package/dist/events/EventComments.svelte.d.ts +1 -1
- package/dist/events/EventView.svelte +5 -0
- package/dist/events/EventView.svelte.d.ts +3 -0
- package/dist/events/event-link-embed.d.ts +4 -1
- package/dist/events/event-link-embed.js +9 -1
- package/dist/events/event-view/EventLinkEmbed.svelte +8 -8
- package/dist/events/event-view/EventLinkEmbed.svelte.d.ts +3 -2
- package/dist/icon-maker/artwork.d.ts +11 -0
- package/dist/icon-maker/artwork.js +25 -0
- package/dist/icon-maker/index.d.ts +12 -0
- package/dist/icon-maker/index.js +73 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.js +7 -1
- package/dist/palettes.d.ts +20 -0
- package/dist/palettes.js +37 -0
- package/dist/richtext/article-styles.d.ts +10 -0
- package/dist/richtext/article-styles.js +28 -0
- package/dist/richtext/document.d.ts +63 -1
- package/dist/richtext/document.js +54 -0
- package/dist/richtext/markdown.d.ts +19 -4
- package/dist/richtext/markdown.js +223 -18
- package/dist/richtext/serialize.d.ts +5 -1
- package/dist/richtext/serialize.js +163 -31
- package/dist/richtext/vendor/markdown-tree.d.ts +4 -3
- package/dist/richtext/vendor/markdown-tree.js +2584 -26
- package/dist/richtext.d.ts +7 -5
- package/dist/richtext.js +1 -1
- package/dist/social/external-embeds.d.ts +5 -0
- package/dist/social/external-embeds.js +6 -1
- package/dist/social/posts.d.ts +2 -0
- package/dist/social/posts.js +43 -2
- package/dist/social/profile.js +2 -1
- package/package.json +28 -2
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
import { type EditorState } from '@tiptap/pm/state';
|
|
3
|
+
import { type IconNode } from '@sketchyicons/svelte';
|
|
4
|
+
/** What the commands need from the app around the editor. */
|
|
5
|
+
export type ArticleCommandContext = {
|
|
6
|
+
/** Opens the file picker; undefined when images cannot be added. */
|
|
7
|
+
pickImages?: () => void;
|
|
8
|
+
};
|
|
9
|
+
export type ArticleCommand = {
|
|
10
|
+
id: string;
|
|
11
|
+
label: string;
|
|
12
|
+
icon: IconNode;
|
|
13
|
+
/** What to type to get the same, shown beside the label. */
|
|
14
|
+
hint?: string;
|
|
15
|
+
/** More words to find it by. */
|
|
16
|
+
keywords?: string;
|
|
17
|
+
/**
|
|
18
|
+
* `block` turns the current block into something, `insert` adds something new, `table`
|
|
19
|
+
* changes the table the cursor is in.
|
|
20
|
+
*/
|
|
21
|
+
kind: 'block' | 'insert' | 'table';
|
|
22
|
+
available?: (context: ArticleCommandContext, editor: Editor) => boolean;
|
|
23
|
+
run: (editor: Editor, context: ArticleCommandContext) => void;
|
|
24
|
+
};
|
|
25
|
+
export type ArticleMark = {
|
|
26
|
+
id: string;
|
|
27
|
+
label: string;
|
|
28
|
+
icon: IconNode;
|
|
29
|
+
/** The key pressed with Cmd or Ctrl; uppercase also needs Shift. */
|
|
30
|
+
key: string;
|
|
31
|
+
run: (editor: Editor) => void;
|
|
32
|
+
};
|
|
33
|
+
export declare function inTable(state: EditorState): boolean;
|
|
34
|
+
/** The id of the `block` command the cursor's block is: what it is nearest wrapped in. */
|
|
35
|
+
export declare function currentBlock(state: EditorState): string;
|
|
36
|
+
/** Everything the slash menu offers; the selection menu and the toolbar use parts of it. */
|
|
37
|
+
export declare const ARTICLE_COMMANDS: readonly ArticleCommand[];
|
|
38
|
+
export declare const ARTICLE_MARKS: readonly ArticleMark[];
|
|
39
|
+
/** The commands that apply where the cursor is: a table cell holds one line of text. */
|
|
40
|
+
export declare function availableCommands(editor: Editor, context: ArticleCommandContext): ArticleCommand[];
|
|
41
|
+
/** Commands matching what was typed after the slash, best first. */
|
|
42
|
+
export declare function findCommands(query: string, editor: Editor, context: ArticleCommandContext): ArticleCommand[];
|
|
43
|
+
/** Run a command. Turning a block into what it already is changes nothing. */
|
|
44
|
+
export declare function runCommand(editor: Editor, command: ArticleCommand, context: ArticleCommandContext): undefined;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { TextSelection } from '@tiptap/pm/state';
|
|
2
|
+
import { Bold, Code, Heading1, Heading2, Heading3, Highlighter, Image, Info, Italic, List, ListOrdered, ListTodo, Minus, Plus, Sigma, SquareCode, Strikethrough, Superscript, Table, TextQuote, Trash2, Type } from '@sketchyicons/svelte';
|
|
3
|
+
import { isCallout } from '../richtext/document.js';
|
|
4
|
+
import { FOOTNOTE_DEFINITION, FOOTNOTE_REFERENCE, HIGHLIGHT, MATH } from './document.js';
|
|
5
|
+
const focus = (editor) => editor.chain().focus();
|
|
6
|
+
export function inTable(state) {
|
|
7
|
+
const { $from } = state.selection;
|
|
8
|
+
for (let depth = $from.depth; depth > 0; depth--)
|
|
9
|
+
if ($from.node(depth).type.name === 'table')
|
|
10
|
+
return true;
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
/** The id of the `block` command the cursor's block is: what it is nearest wrapped in. */
|
|
14
|
+
export function currentBlock(state) {
|
|
15
|
+
const { $from } = state.selection;
|
|
16
|
+
const block = $from.parent;
|
|
17
|
+
if (block.type.name === 'codeBlock')
|
|
18
|
+
return block.attrs.language === MATH ? 'math' : 'code';
|
|
19
|
+
if (block.type.name === 'heading')
|
|
20
|
+
return `h${block.attrs.level}`;
|
|
21
|
+
for (let depth = $from.depth - 1; depth > 0; depth--) {
|
|
22
|
+
const node = $from.node(depth);
|
|
23
|
+
switch (node.type.name) {
|
|
24
|
+
case 'bulletList':
|
|
25
|
+
return 'bullet';
|
|
26
|
+
case 'orderedList':
|
|
27
|
+
return 'ordered';
|
|
28
|
+
case 'taskList':
|
|
29
|
+
return 'todo';
|
|
30
|
+
case 'blockquote':
|
|
31
|
+
return isCallout(node.attrs.callout) ? 'callout' : 'quote';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return 'text';
|
|
35
|
+
}
|
|
36
|
+
const heading = (level, icon) => ({
|
|
37
|
+
id: `h${level}`,
|
|
38
|
+
label: `Heading ${level}`,
|
|
39
|
+
icon,
|
|
40
|
+
hint: '#'.repeat(level),
|
|
41
|
+
keywords: 'title',
|
|
42
|
+
kind: 'block',
|
|
43
|
+
run: (editor) => focus(editor).setHeading({ level }).run()
|
|
44
|
+
});
|
|
45
|
+
const quote = (callout) => (editor) => {
|
|
46
|
+
if (editor.isActive('blockquote'))
|
|
47
|
+
focus(editor).updateAttributes('blockquote', { callout }).run();
|
|
48
|
+
else
|
|
49
|
+
focus(editor).wrapIn('blockquote', { callout }).run();
|
|
50
|
+
};
|
|
51
|
+
/** A list item starts with a line of text, so its first line cannot become a quote. */
|
|
52
|
+
const canQuote = (_context, editor) => editor.isActive('blockquote') || editor.can().wrapIn('blockquote');
|
|
53
|
+
/** A reference at the cursor with the next free number, and its note at the end to write in. */
|
|
54
|
+
function insertFootnote(editor) {
|
|
55
|
+
const { state } = editor;
|
|
56
|
+
const reference = state.schema.nodes[FOOTNOTE_REFERENCE];
|
|
57
|
+
const definition = state.schema.nodes[FOOTNOTE_DEFINITION];
|
|
58
|
+
if (!reference || !definition)
|
|
59
|
+
return;
|
|
60
|
+
const used = new Set();
|
|
61
|
+
state.doc.descendants((node) => {
|
|
62
|
+
if (node.type === reference || node.type === definition)
|
|
63
|
+
used.add(String(node.attrs.id));
|
|
64
|
+
});
|
|
65
|
+
let number = 1;
|
|
66
|
+
while (used.has(String(number)))
|
|
67
|
+
number++;
|
|
68
|
+
const id = String(number);
|
|
69
|
+
const tr = state.tr.replaceSelectionWith(reference.create({ id }));
|
|
70
|
+
const end = tr.doc.content.size;
|
|
71
|
+
tr.insert(end, definition.create({ id }, state.schema.nodes.paragraph.create()));
|
|
72
|
+
tr.setSelection(TextSelection.create(tr.doc, end + 2));
|
|
73
|
+
editor.view.dispatch(tr.scrollIntoView());
|
|
74
|
+
editor.view.focus();
|
|
75
|
+
}
|
|
76
|
+
/** Everything the slash menu offers; the selection menu and the toolbar use parts of it. */
|
|
77
|
+
export const ARTICLE_COMMANDS = [
|
|
78
|
+
{
|
|
79
|
+
id: 'text',
|
|
80
|
+
label: 'Text',
|
|
81
|
+
icon: Type,
|
|
82
|
+
keywords: 'paragraph plain',
|
|
83
|
+
kind: 'block',
|
|
84
|
+
run: (editor) => focus(editor).clearNodes().run()
|
|
85
|
+
},
|
|
86
|
+
heading(1, Heading1),
|
|
87
|
+
heading(2, Heading2),
|
|
88
|
+
heading(3, Heading3),
|
|
89
|
+
{
|
|
90
|
+
id: 'bullet',
|
|
91
|
+
label: 'Bulleted list',
|
|
92
|
+
icon: List,
|
|
93
|
+
hint: '-',
|
|
94
|
+
keywords: 'unordered ul',
|
|
95
|
+
kind: 'block',
|
|
96
|
+
run: (editor) => focus(editor).toggleBulletList().run()
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 'ordered',
|
|
100
|
+
label: 'Numbered list',
|
|
101
|
+
icon: ListOrdered,
|
|
102
|
+
hint: '1.',
|
|
103
|
+
keywords: 'ordered ol',
|
|
104
|
+
kind: 'block',
|
|
105
|
+
run: (editor) => focus(editor).toggleOrderedList().run()
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: 'todo',
|
|
109
|
+
label: 'To-do list',
|
|
110
|
+
icon: ListTodo,
|
|
111
|
+
hint: '[ ]',
|
|
112
|
+
keywords: 'task checkbox check',
|
|
113
|
+
kind: 'block',
|
|
114
|
+
run: (editor) => focus(editor).toggleTaskList().run()
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'quote',
|
|
118
|
+
label: 'Quote',
|
|
119
|
+
icon: TextQuote,
|
|
120
|
+
hint: '>',
|
|
121
|
+
keywords: 'blockquote citation',
|
|
122
|
+
kind: 'block',
|
|
123
|
+
available: canQuote,
|
|
124
|
+
run: quote(null)
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
id: 'callout',
|
|
128
|
+
label: 'Callout',
|
|
129
|
+
icon: Info,
|
|
130
|
+
keywords: 'note tip important warning caution alert',
|
|
131
|
+
kind: 'block',
|
|
132
|
+
available: canQuote,
|
|
133
|
+
run: quote('note')
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: 'code',
|
|
137
|
+
label: 'Code',
|
|
138
|
+
icon: SquareCode,
|
|
139
|
+
hint: '```',
|
|
140
|
+
keywords: 'snippet pre',
|
|
141
|
+
kind: 'block',
|
|
142
|
+
// Named, or a formula turned into code would stay one: the block's attributes carry over.
|
|
143
|
+
run: (editor) => focus(editor).setCodeBlock({ language: '' }).run()
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'math',
|
|
147
|
+
label: 'Math',
|
|
148
|
+
icon: Sigma,
|
|
149
|
+
keywords: 'formula equation tex latex',
|
|
150
|
+
kind: 'block',
|
|
151
|
+
run: (editor) => focus(editor).setCodeBlock({ language: MATH }).run()
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
id: 'divider',
|
|
155
|
+
label: 'Divider',
|
|
156
|
+
icon: Minus,
|
|
157
|
+
hint: '---',
|
|
158
|
+
keywords: 'horizontal rule line separator hr',
|
|
159
|
+
kind: 'insert',
|
|
160
|
+
run: (editor) => focus(editor).setHorizontalRule().run()
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: 'image',
|
|
164
|
+
label: 'Image',
|
|
165
|
+
icon: Image,
|
|
166
|
+
keywords: 'picture photo upload',
|
|
167
|
+
kind: 'insert',
|
|
168
|
+
available: (context) => !!context.pickImages,
|
|
169
|
+
run: (_editor, context) => context.pickImages?.()
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: 'table',
|
|
173
|
+
label: 'Table',
|
|
174
|
+
icon: Table,
|
|
175
|
+
keywords: 'grid rows columns',
|
|
176
|
+
kind: 'insert',
|
|
177
|
+
run: (editor) => focus(editor).insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run()
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
id: 'footnote',
|
|
181
|
+
label: 'Footnote',
|
|
182
|
+
icon: Superscript,
|
|
183
|
+
hint: '[^1]',
|
|
184
|
+
keywords: 'note reference source',
|
|
185
|
+
kind: 'insert',
|
|
186
|
+
run: insertFootnote
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
id: 'row',
|
|
190
|
+
label: 'Add row',
|
|
191
|
+
icon: Plus,
|
|
192
|
+
keywords: 'table below',
|
|
193
|
+
kind: 'table',
|
|
194
|
+
run: (editor) => focus(editor).addRowAfter().run()
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
id: 'column',
|
|
198
|
+
label: 'Add column',
|
|
199
|
+
icon: Plus,
|
|
200
|
+
keywords: 'table right',
|
|
201
|
+
kind: 'table',
|
|
202
|
+
run: (editor) => focus(editor).addColumnAfter().run()
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
id: 'deleteRow',
|
|
206
|
+
label: 'Delete row',
|
|
207
|
+
icon: Minus,
|
|
208
|
+
keywords: 'table remove',
|
|
209
|
+
kind: 'table',
|
|
210
|
+
run: (editor) => focus(editor).deleteRow().run()
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
id: 'deleteColumn',
|
|
214
|
+
label: 'Delete column',
|
|
215
|
+
icon: Minus,
|
|
216
|
+
keywords: 'table remove',
|
|
217
|
+
kind: 'table',
|
|
218
|
+
run: (editor) => focus(editor).deleteColumn().run()
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
id: 'deleteTable',
|
|
222
|
+
label: 'Delete table',
|
|
223
|
+
icon: Trash2,
|
|
224
|
+
keywords: 'remove',
|
|
225
|
+
kind: 'table',
|
|
226
|
+
run: (editor) => focus(editor).deleteTable().run()
|
|
227
|
+
}
|
|
228
|
+
];
|
|
229
|
+
export const ARTICLE_MARKS = [
|
|
230
|
+
{ id: 'bold', label: 'Bold', icon: Bold, key: 'b', run: (e) => focus(e).toggleBold().run() },
|
|
231
|
+
{
|
|
232
|
+
id: 'italic',
|
|
233
|
+
label: 'Italic',
|
|
234
|
+
icon: Italic,
|
|
235
|
+
key: 'i',
|
|
236
|
+
run: (e) => focus(e).toggleItalic().run()
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
id: 'strike',
|
|
240
|
+
label: 'Strikethrough',
|
|
241
|
+
icon: Strikethrough,
|
|
242
|
+
key: 'S',
|
|
243
|
+
run: (e) => focus(e).toggleStrike().run()
|
|
244
|
+
},
|
|
245
|
+
{ id: 'code', label: 'Code', icon: Code, key: 'e', run: (e) => focus(e).toggleCode().run() },
|
|
246
|
+
{
|
|
247
|
+
id: HIGHLIGHT,
|
|
248
|
+
label: 'Highlight',
|
|
249
|
+
icon: Highlighter,
|
|
250
|
+
key: 'H',
|
|
251
|
+
run: (e) => focus(e).toggleMark(HIGHLIGHT).run()
|
|
252
|
+
}
|
|
253
|
+
];
|
|
254
|
+
/** The commands that apply where the cursor is: a table cell holds one line of text. */
|
|
255
|
+
export function availableCommands(editor, context) {
|
|
256
|
+
const table = inTable(editor.state);
|
|
257
|
+
return ARTICLE_COMMANDS.filter((command) => (command.kind === 'table') === table && (command.available?.(context, editor) ?? true));
|
|
258
|
+
}
|
|
259
|
+
/** Commands matching what was typed after the slash, best first. */
|
|
260
|
+
export function findCommands(query, editor, context) {
|
|
261
|
+
// "/ " is a slash in the text, not a search.
|
|
262
|
+
if (/^\s/.test(query))
|
|
263
|
+
return [];
|
|
264
|
+
const all = availableCommands(editor, context);
|
|
265
|
+
const wanted = query.trim().toLowerCase();
|
|
266
|
+
if (!wanted)
|
|
267
|
+
return all;
|
|
268
|
+
const rank = (command) => {
|
|
269
|
+
const label = command.label.toLowerCase();
|
|
270
|
+
if (label.startsWith(wanted) || command.id.toLowerCase() === wanted)
|
|
271
|
+
return 0;
|
|
272
|
+
if (label.split(' ').some((word) => word.startsWith(wanted)))
|
|
273
|
+
return 1;
|
|
274
|
+
if ((command.keywords ?? '').split(' ').some((word) => word.startsWith(wanted)))
|
|
275
|
+
return 2;
|
|
276
|
+
return label.includes(wanted) ? 3 : -1;
|
|
277
|
+
};
|
|
278
|
+
return all
|
|
279
|
+
.map((command) => ({ command, rank: rank(command) }))
|
|
280
|
+
.filter((entry) => entry.rank >= 0)
|
|
281
|
+
.sort((a, b) => a.rank - b.rank)
|
|
282
|
+
.map((entry) => entry.command);
|
|
283
|
+
}
|
|
284
|
+
/** Run a command. Turning a block into what it already is changes nothing. */
|
|
285
|
+
export function runCommand(editor, command, context) {
|
|
286
|
+
if (command.kind === 'block' && currentBlock(editor.state) === command.id)
|
|
287
|
+
return void editor.commands.focus();
|
|
288
|
+
command.run(editor, context);
|
|
289
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Node, type AnyExtension } from '@tiptap/core';
|
|
2
|
+
/** A task in a numbered or mixed list: the box is kept and shown, the list stays what it is. */
|
|
3
|
+
export declare const CheckableListItem: Node<import("@tiptap/extension-list").ListItemOptions, any>;
|
|
4
|
+
/**
|
|
5
|
+
* What article Markdown has beyond the basics: task lists, math, footnotes, highlights,
|
|
6
|
+
* callouts and code languages. The quote and code block here replace the starter kit's.
|
|
7
|
+
*/
|
|
8
|
+
export declare function articleExtensions(): AnyExtension[];
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { InputRule, Mark, markInputRule, Node } from '@tiptap/core';
|
|
2
|
+
import Blockquote from '@tiptap/extension-blockquote';
|
|
3
|
+
import CodeBlock from '@tiptap/extension-code-block';
|
|
4
|
+
import { ListItem, TaskItem, TaskList } from '@tiptap/extension-list';
|
|
5
|
+
import { CALLOUT_ICONS, CALLOUT_LABELS, calloutClass, calloutLabelClass, highlightClass } from '../richtext/article-styles.js';
|
|
6
|
+
import { CALLOUTS, isCallout } from '../richtext/document.js';
|
|
7
|
+
import { FOOTNOTE_ID } from '../richtext/markdown.js';
|
|
8
|
+
import { FOOTNOTE_DEFINITION, FOOTNOTE_REFERENCE, HIGHLIGHT, MATH } from './document.js';
|
|
9
|
+
/** `==marked==` text. Typing it makes one. */
|
|
10
|
+
const HighlightMark = Mark.create({
|
|
11
|
+
name: HIGHLIGHT,
|
|
12
|
+
parseHTML: () => [{ tag: 'mark' }],
|
|
13
|
+
renderHTML: () => ['mark', { class: highlightClass }, 0],
|
|
14
|
+
addInputRules() {
|
|
15
|
+
return [markInputRule({ find: /(?:^|\s)(==([^=\s](?:[^=]*[^=\s])?)==)$/, type: this.type })];
|
|
16
|
+
},
|
|
17
|
+
addKeyboardShortcuts() {
|
|
18
|
+
return { 'Mod-Shift-h': () => this.editor.commands.toggleMark(this.name) };
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
function iconElement(icon, size) {
|
|
22
|
+
const namespace = 'http://www.w3.org/2000/svg';
|
|
23
|
+
const svg = document.createElementNS(namespace, 'svg');
|
|
24
|
+
const attributes = {
|
|
25
|
+
viewBox: '0 0 24 24',
|
|
26
|
+
width: size,
|
|
27
|
+
height: size,
|
|
28
|
+
fill: 'none',
|
|
29
|
+
stroke: 'currentColor',
|
|
30
|
+
'stroke-width': 1.75,
|
|
31
|
+
'stroke-linecap': 'round',
|
|
32
|
+
'stroke-linejoin': 'round',
|
|
33
|
+
'aria-hidden': 'true'
|
|
34
|
+
};
|
|
35
|
+
for (const [name, value] of Object.entries(attributes))
|
|
36
|
+
svg.setAttribute(name, String(value));
|
|
37
|
+
for (const [, drawn] of icon) {
|
|
38
|
+
const path = document.createElementNS(namespace, 'path');
|
|
39
|
+
path.setAttribute('d', String(drawn.d));
|
|
40
|
+
path.setAttribute('fill', String(drawn.fill ?? 'none'));
|
|
41
|
+
svg.append(path);
|
|
42
|
+
}
|
|
43
|
+
return svg;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A quote, or with `callout` one of GitHub's five callouts, written `> [!NOTE]`. Typing
|
|
47
|
+
* `[!note] ` at the start of a quote makes one; its label changes the kind.
|
|
48
|
+
*/
|
|
49
|
+
const CalloutQuote = Blockquote.extend({
|
|
50
|
+
addAttributes() {
|
|
51
|
+
return {
|
|
52
|
+
callout: {
|
|
53
|
+
default: null,
|
|
54
|
+
parseHTML: (element) => {
|
|
55
|
+
const kind = element.getAttribute('data-callout');
|
|
56
|
+
return isCallout(kind) ? kind : null;
|
|
57
|
+
},
|
|
58
|
+
renderHTML: (attributes) => isCallout(attributes.callout) ? { 'data-callout': attributes.callout } : {}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
addInputRules() {
|
|
63
|
+
const type = this.type;
|
|
64
|
+
return [
|
|
65
|
+
...(this.parent?.() ?? []),
|
|
66
|
+
new InputRule({
|
|
67
|
+
find: /^\[!(note|tip|important|warning|caution)\]\s$/i,
|
|
68
|
+
handler: ({ state, range, match }) => {
|
|
69
|
+
const { $from } = state.selection;
|
|
70
|
+
const quote = $from.depth > 1 ? $from.node($from.depth - 1) : undefined;
|
|
71
|
+
if (quote?.type !== type || $from.index($from.depth - 1) !== 0)
|
|
72
|
+
return null;
|
|
73
|
+
state.tr
|
|
74
|
+
.delete(range.from, range.to)
|
|
75
|
+
.setNodeMarkup($from.before($from.depth - 1), undefined, {
|
|
76
|
+
...quote.attrs,
|
|
77
|
+
callout: match[1].toLowerCase()
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
];
|
|
82
|
+
},
|
|
83
|
+
addNodeView() {
|
|
84
|
+
return ({ node, editor, getPos }) => {
|
|
85
|
+
const kind = node.attrs.callout;
|
|
86
|
+
if (!isCallout(kind)) {
|
|
87
|
+
const dom = document.createElement('blockquote');
|
|
88
|
+
return {
|
|
89
|
+
dom,
|
|
90
|
+
contentDOM: dom,
|
|
91
|
+
update: (next) => next.type === node.type && !isCallout(next.attrs.callout)
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const dom = document.createElement('div');
|
|
95
|
+
dom.dataset.callout = kind;
|
|
96
|
+
dom.className = calloutClass(kind);
|
|
97
|
+
const label = document.createElement('label');
|
|
98
|
+
label.contentEditable = 'false';
|
|
99
|
+
label.className = `${calloutLabelClass(kind)} w-fit`;
|
|
100
|
+
const select = document.createElement('select');
|
|
101
|
+
select.setAttribute('aria-label', 'Callout type');
|
|
102
|
+
select.className =
|
|
103
|
+
'cursor-pointer appearance-none field-sizing-content rounded-sm border-0 bg-transparent bg-none p-0 font-semibold text-inherit focus:ring-0';
|
|
104
|
+
for (const [value, text] of [
|
|
105
|
+
...CALLOUTS.map((item) => [item, CALLOUT_LABELS[item]]),
|
|
106
|
+
['', 'Quote']
|
|
107
|
+
]) {
|
|
108
|
+
const option = document.createElement('option');
|
|
109
|
+
option.value = value;
|
|
110
|
+
option.textContent = text;
|
|
111
|
+
select.append(option);
|
|
112
|
+
}
|
|
113
|
+
select.value = kind;
|
|
114
|
+
select.disabled = !editor.isEditable;
|
|
115
|
+
select.addEventListener('change', () => {
|
|
116
|
+
const pos = getPos();
|
|
117
|
+
if (typeof pos !== 'number' || !editor.isEditable)
|
|
118
|
+
return;
|
|
119
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, {
|
|
120
|
+
...node.attrs,
|
|
121
|
+
callout: select.value || null
|
|
122
|
+
}));
|
|
123
|
+
editor.view.focus();
|
|
124
|
+
});
|
|
125
|
+
label.append(iconElement(CALLOUT_ICONS[kind], 16), select);
|
|
126
|
+
const content = document.createElement('div');
|
|
127
|
+
content.className = 'space-y-3';
|
|
128
|
+
dom.append(label, content);
|
|
129
|
+
return {
|
|
130
|
+
dom,
|
|
131
|
+
contentDOM: content,
|
|
132
|
+
update: (next) => next.type === node.type && next.attrs.callout === kind,
|
|
133
|
+
stopEvent: (event) => event.target instanceof Element && label.contains(event.target),
|
|
134
|
+
ignoreMutation: (mutation) => mutation.type !== 'selection' && !content.contains(mutation.target)
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
/** A code block with its language written in its corner. "math" is a displayed formula. */
|
|
140
|
+
const LanguageCodeBlock = CodeBlock.extend({
|
|
141
|
+
addNodeView() {
|
|
142
|
+
return ({ node, editor, getPos }) => {
|
|
143
|
+
let current = node;
|
|
144
|
+
const dom = document.createElement('div');
|
|
145
|
+
dom.className = 'relative';
|
|
146
|
+
const pre = document.createElement('pre');
|
|
147
|
+
pre.className = 'pt-9!';
|
|
148
|
+
const code = document.createElement('code');
|
|
149
|
+
pre.append(code);
|
|
150
|
+
const input = document.createElement('input');
|
|
151
|
+
input.type = 'text';
|
|
152
|
+
input.placeholder = 'Language';
|
|
153
|
+
input.setAttribute('aria-label', 'Language');
|
|
154
|
+
input.spellcheck = false;
|
|
155
|
+
input.autocapitalize = 'off';
|
|
156
|
+
input.className =
|
|
157
|
+
'absolute top-2.5 left-3 w-40 max-w-[calc(100%-1.5rem)] border-0 bg-transparent p-0 font-sans text-xs text-base-600 placeholder:text-base-500 focus:ring-0 focus:outline-none dark:text-base-400';
|
|
158
|
+
dom.append(pre, input);
|
|
159
|
+
const show = () => {
|
|
160
|
+
const language = typeof current.attrs.language === 'string' ? current.attrs.language : '';
|
|
161
|
+
code.className = language ? `${this.options.languageClassPrefix ?? ''}${language}` : '';
|
|
162
|
+
if (document.activeElement !== input)
|
|
163
|
+
input.value = language;
|
|
164
|
+
input.readOnly = !editor.isEditable;
|
|
165
|
+
};
|
|
166
|
+
show();
|
|
167
|
+
input.addEventListener('input', () => {
|
|
168
|
+
const pos = getPos();
|
|
169
|
+
if (typeof pos !== 'number' || !editor.isEditable)
|
|
170
|
+
return;
|
|
171
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, {
|
|
172
|
+
...current.attrs,
|
|
173
|
+
language: input.value.replace(/[`\s]/g, '') || null
|
|
174
|
+
}));
|
|
175
|
+
});
|
|
176
|
+
input.addEventListener('keydown', (event) => {
|
|
177
|
+
if (!['Enter', 'Escape', 'ArrowDown'].includes(event.key))
|
|
178
|
+
return;
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
const pos = getPos();
|
|
181
|
+
if (typeof pos === 'number')
|
|
182
|
+
editor
|
|
183
|
+
.chain()
|
|
184
|
+
.focus()
|
|
185
|
+
.setTextSelection(pos + 1)
|
|
186
|
+
.run();
|
|
187
|
+
});
|
|
188
|
+
return {
|
|
189
|
+
dom,
|
|
190
|
+
contentDOM: code,
|
|
191
|
+
update: (next) => {
|
|
192
|
+
if (next.type !== current.type)
|
|
193
|
+
return false;
|
|
194
|
+
current = next;
|
|
195
|
+
show();
|
|
196
|
+
return true;
|
|
197
|
+
},
|
|
198
|
+
stopEvent: (event) => event.target === input,
|
|
199
|
+
ignoreMutation: (mutation) => mutation.type !== 'selection' && !code.contains(mutation.target)
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
/** TeX source in the line, written to Markdown between dollars. Typing `$x^2$` makes one. */
|
|
205
|
+
const MathMark = Mark.create({
|
|
206
|
+
name: MATH,
|
|
207
|
+
code: true,
|
|
208
|
+
excludes: '_',
|
|
209
|
+
inclusive: false,
|
|
210
|
+
parseHTML: () => [{ tag: 'span[data-math]' }],
|
|
211
|
+
renderHTML: () => ['span', { 'data-math': '' }, 0],
|
|
212
|
+
addInputRules() {
|
|
213
|
+
return [
|
|
214
|
+
// The dollars hug the formula, as when reading: "$5 and $" starts nothing.
|
|
215
|
+
markInputRule({ find: /(?:^|\s)(\$([^$\s](?:[^$]*[^$\s])?)\$)$/, type: this.type })
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
/** `[^id]` in the text. Typing it makes one. */
|
|
220
|
+
const FootnoteReference = Node.create({
|
|
221
|
+
name: FOOTNOTE_REFERENCE,
|
|
222
|
+
group: 'inline',
|
|
223
|
+
inline: true,
|
|
224
|
+
atom: true,
|
|
225
|
+
selectable: true,
|
|
226
|
+
addAttributes: () => ({ id: { default: '' } }),
|
|
227
|
+
parseHTML: () => [
|
|
228
|
+
{
|
|
229
|
+
tag: 'sup[data-footnote-ref]',
|
|
230
|
+
getAttrs: (element) => ({ id: element.getAttribute('data-footnote-ref') ?? '' })
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
renderHTML: ({ node }) => ['sup', { 'data-footnote-ref': node.attrs.id }, `[${node.attrs.id}]`],
|
|
234
|
+
renderText: ({ node }) => `[^${node.attrs.id}]`,
|
|
235
|
+
addInputRules() {
|
|
236
|
+
const type = this.type;
|
|
237
|
+
return [
|
|
238
|
+
new InputRule({
|
|
239
|
+
find: /\[\^([^\s[\]^]{1,100})\]$/,
|
|
240
|
+
handler: ({ state, range, match }) => {
|
|
241
|
+
state.tr.replaceWith(range.from, range.to, type.create({ id: match[1] }));
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
];
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
/** The note a reference points to. Typing `: ` after a reference at the start of a line makes one. */
|
|
248
|
+
const FootnoteDefinition = Node.create({
|
|
249
|
+
name: FOOTNOTE_DEFINITION,
|
|
250
|
+
group: 'block',
|
|
251
|
+
content: 'block+',
|
|
252
|
+
defining: true,
|
|
253
|
+
addAttributes: () => ({ id: { default: '' } }),
|
|
254
|
+
parseHTML: () => [
|
|
255
|
+
{
|
|
256
|
+
tag: 'div[data-footnote]',
|
|
257
|
+
getAttrs: (element) => ({ id: element.getAttribute('data-footnote') ?? '' })
|
|
258
|
+
}
|
|
259
|
+
],
|
|
260
|
+
renderHTML: ({ node }) => ['div', { 'data-footnote': node.attrs.id }, 0],
|
|
261
|
+
addInputRules() {
|
|
262
|
+
const type = this.type;
|
|
263
|
+
return [
|
|
264
|
+
new InputRule({
|
|
265
|
+
find: /: $/,
|
|
266
|
+
handler: ({ state }) => {
|
|
267
|
+
const { $from, empty } = state.selection;
|
|
268
|
+
const paragraph = $from.parent;
|
|
269
|
+
const reference = paragraph.firstChild;
|
|
270
|
+
// Exactly a reference, then the colon and space just typed.
|
|
271
|
+
if (!empty ||
|
|
272
|
+
$from.depth !== 1 ||
|
|
273
|
+
paragraph.type.name !== 'paragraph' ||
|
|
274
|
+
paragraph.childCount !== 2 ||
|
|
275
|
+
reference?.type.name !== FOOTNOTE_REFERENCE ||
|
|
276
|
+
!/^: ?$/.test(paragraph.lastChild?.text ?? '') ||
|
|
277
|
+
!FOOTNOTE_ID.test(reference.attrs.id))
|
|
278
|
+
return null;
|
|
279
|
+
const from = $from.before();
|
|
280
|
+
state.tr.replaceWith(from, $from.after(), type.create({ id: reference.attrs.id }, state.schema.nodes.paragraph.create()));
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
];
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
/** A task in a numbered or mixed list: the box is kept and shown, the list stays what it is. */
|
|
287
|
+
export const CheckableListItem = ListItem.extend({
|
|
288
|
+
addAttributes() {
|
|
289
|
+
return {
|
|
290
|
+
...this.parent?.(),
|
|
291
|
+
checked: {
|
|
292
|
+
default: null,
|
|
293
|
+
parseHTML: (element) => element.hasAttribute('data-task') ? element.getAttribute('data-task') === 'true' : null,
|
|
294
|
+
renderHTML: (attributes) => typeof attributes.checked === 'boolean' ? { 'data-task': String(attributes.checked) } : {}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
/**
|
|
300
|
+
* What article Markdown has beyond the basics: task lists, math, footnotes, highlights,
|
|
301
|
+
* callouts and code languages. The quote and code block here replace the starter kit's.
|
|
302
|
+
*/
|
|
303
|
+
export function articleExtensions() {
|
|
304
|
+
return [
|
|
305
|
+
HighlightMark,
|
|
306
|
+
CalloutQuote,
|
|
307
|
+
LanguageCodeBlock,
|
|
308
|
+
CheckableListItem,
|
|
309
|
+
TaskList,
|
|
310
|
+
TaskItem.configure({ nested: true }),
|
|
311
|
+
MathMark,
|
|
312
|
+
FootnoteReference,
|
|
313
|
+
FootnoteDefinition
|
|
314
|
+
];
|
|
315
|
+
}
|
|
@@ -2,7 +2,12 @@ import type { JSONContent } from '@tiptap/core';
|
|
|
2
2
|
import { type RichTextDocument } from '../richtext/document.js';
|
|
3
3
|
import type { Facet } from '../social/richtext.js';
|
|
4
4
|
export declare const LINK_EMBED = "linkEmbed";
|
|
5
|
+
export declare const IMAGE = "image";
|
|
6
|
+
export declare const MATH = "math";
|
|
7
|
+
export declare const FOOTNOTE_REFERENCE = "footnoteReference";
|
|
8
|
+
export declare const FOOTNOTE_DEFINITION = "footnoteDefinition";
|
|
5
9
|
type EmbedTest = ((url: string) => boolean) | undefined;
|
|
10
|
+
export declare const HIGHLIGHT = "highlight";
|
|
6
11
|
/** A URL alone, e.g. the whole text of a line; undefined for anything else. */
|
|
7
12
|
export declare function bareUrl(text: string): string | undefined;
|
|
8
13
|
/** A parsed document as editor content. Links that would be shown as embeds become embed blocks. */
|