@axium/rich-text 0.0.0-stage → 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.md +157 -0
- package/README.md +64 -2
- package/dist/.locale.d.ts +17 -0
- package/dist/.locale.js +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +74 -0
- package/lib/NodeView.svelte +8 -0
- package/lib/RichText.svelte +389 -0
- package/lib/SelectionMenu.svelte +99 -0
- package/lib/SuggestionList.svelte +76 -0
- package/lib/actions.ts +58 -0
- package/lib/extensions.ts +26 -0
- package/lib/index.ts +7 -0
- package/lib/locales.ts +4 -0
- package/lib/nodes.svelte.ts +238 -0
- package/lib/serialize.ts +22 -0
- package/lib/task-item.ts +78 -0
- package/lib/tsconfig.json +12 -0
- package/locales/en.json +16 -0
- package/package.json +50 -5
package/LICENSE.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2007 Free Software Foundation, Inc.
|
|
6
|
+
<https://fsf.org/>
|
|
7
|
+
|
|
8
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
9
|
+
license document, but changing it is not allowed.
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates the
|
|
12
|
+
terms and conditions of version 3 of the GNU General Public License,
|
|
13
|
+
supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
## 0. Additional Definitions.
|
|
16
|
+
|
|
17
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the "GNU GPL" refers to version 3 of the
|
|
19
|
+
GNU General Public License.
|
|
20
|
+
|
|
21
|
+
"The Library" refers to a covered work governed by this License, other
|
|
22
|
+
than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An "Application" is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the "Linked
|
|
32
|
+
Version".
|
|
33
|
+
|
|
34
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
## 2. Conveying Modified Versions.
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
- a) under this License, provided that you make a good faith effort
|
|
58
|
+
to ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
+
this License applicable to that copy.
|
|
63
|
+
|
|
64
|
+
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
+
|
|
66
|
+
The object code form of an Application may incorporate material from a
|
|
67
|
+
header file that is part of the Library. You may convey such object
|
|
68
|
+
code under terms of your choice, provided that, if the incorporated
|
|
69
|
+
material is not limited to numerical parameters, data structure
|
|
70
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
+
(ten or fewer lines in length), you do both of the following:
|
|
72
|
+
|
|
73
|
+
- a) Give prominent notice with each copy of the object code that
|
|
74
|
+
the Library is used in it and that the Library and its use are
|
|
75
|
+
covered by this License.
|
|
76
|
+
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
+
license document.
|
|
78
|
+
|
|
79
|
+
## 4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
+
together, effectively do not restrict modification of the portions of
|
|
83
|
+
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
+
debugging such modifications, if you also do each of the following:
|
|
85
|
+
|
|
86
|
+
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
+
the Library is used in it and that the Library and its use are
|
|
88
|
+
covered by this License.
|
|
89
|
+
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
+
license document.
|
|
91
|
+
- c) For a Combined Work that displays copyright notices during
|
|
92
|
+
execution, include the copyright notice for the Library among
|
|
93
|
+
these notices, as well as a reference directing the user to the
|
|
94
|
+
copies of the GNU GPL and this license document.
|
|
95
|
+
- d) Do one of the following:
|
|
96
|
+
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
+
this License, and the Corresponding Application Code in a form
|
|
98
|
+
suitable for, and under terms that permit, the user to
|
|
99
|
+
recombine or relink the Application with a modified version of
|
|
100
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
+
Corresponding Source.
|
|
103
|
+
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
+
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
+
time a copy of the Library already present on the user's
|
|
106
|
+
computer system, and (b) will operate properly with a modified
|
|
107
|
+
version of the Library that is interface-compatible with the
|
|
108
|
+
Linked Version.
|
|
109
|
+
- e) Provide Installation Information, but only if you would
|
|
110
|
+
otherwise be required to provide such information under section 6
|
|
111
|
+
of the GNU GPL, and only to the extent that such information is
|
|
112
|
+
necessary to install and execute a modified version of the
|
|
113
|
+
Combined Work produced by recombining or relinking the Application
|
|
114
|
+
with a modified version of the Linked Version. (If you use option
|
|
115
|
+
4d0, the Installation Information must accompany the Minimal
|
|
116
|
+
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
+
use option 4d1, you must provide the Installation Information in
|
|
118
|
+
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
+
Corresponding Source.)
|
|
120
|
+
|
|
121
|
+
## 5. Combined Libraries.
|
|
122
|
+
|
|
123
|
+
You may place library facilities that are a work based on the Library
|
|
124
|
+
side by side in a single library together with other library
|
|
125
|
+
facilities that are not Applications and are not covered by this
|
|
126
|
+
License, and convey such a combined library under terms of your
|
|
127
|
+
choice, if you do both of the following:
|
|
128
|
+
|
|
129
|
+
- a) Accompany the combined library with a copy of the same work
|
|
130
|
+
based on the Library, uncombined with any other library
|
|
131
|
+
facilities, conveyed under the terms of this License.
|
|
132
|
+
- b) Give prominent notice with the combined library that part of it
|
|
133
|
+
is a work based on the Library, and explaining where to find the
|
|
134
|
+
accompanying uncombined form of the same work.
|
|
135
|
+
|
|
136
|
+
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
+
|
|
138
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
+
versions will be similar in spirit to the present version, but may
|
|
141
|
+
differ in detail to address new problems or concerns.
|
|
142
|
+
|
|
143
|
+
Each version is given a distinguishing version number. If the Library
|
|
144
|
+
as you received it specifies that a certain numbered version of the
|
|
145
|
+
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
+
it, you have the option of following the terms and conditions either
|
|
147
|
+
of that published version or of any later version published by the
|
|
148
|
+
Free Software Foundation. If the Library as you received it does not
|
|
149
|
+
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
+
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
+
published by the Free Software Foundation.
|
|
152
|
+
|
|
153
|
+
If the Library as you received it specifies that a proxy can decide
|
|
154
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
+
permanent authorization for you to choose that version for the
|
|
157
|
+
Library.
|
package/README.md
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Axium Rich Text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A rich text editor for Axium, built on [Tiptap](https://tiptap.dev).
|
|
4
|
+
|
|
5
|
+
Documents are stored as Tiptap's JSON document model. `@axium/rich-text` has the document schema and helpers for walking and transforming documents, and doesn't depend on the editor, so servers can use it. `@axium/rich-text/components` has the editor.
|
|
6
|
+
|
|
7
|
+
## Editor
|
|
8
|
+
|
|
9
|
+
```svelte
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import { RichText } from '@axium/rich-text/components';
|
|
12
|
+
|
|
13
|
+
let editor: RichText;
|
|
14
|
+
</script>
|
|
15
|
+
|
|
16
|
+
<RichText bind:this={editor} bind:content={note.content} editable={canEdit} onchange={save} />
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- `preset`: `document` (the default) or `email`, which leaves out task lists.
|
|
20
|
+
- `delay`: how long after the last edit `onchange` is called, in milliseconds.
|
|
21
|
+
- `editor.flush()` calls `onchange` immediately with changes still waiting for the delay. The editor also flushes when it is destroyed.
|
|
22
|
+
- Replacing `content` resets the editor. Use `updateNodes` to change custom nodes in place.
|
|
23
|
+
|
|
24
|
+
## Custom nodes
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const taskRef = defineNode({
|
|
28
|
+
name: 'task-ref',
|
|
29
|
+
schema: z.object({ id: z.string(), title: z.string() }),
|
|
30
|
+
render: taskRefSnippet,
|
|
31
|
+
text: data => data.title,
|
|
32
|
+
html: data => ({ tag: 'a', attributes: { href: `/tasks/${data.id}` } }),
|
|
33
|
+
suggest: { char: '#', items: query => searchTasks(query) },
|
|
34
|
+
paste: data => ({ ...data, id: crypto.randomUUID() }),
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Nodes are atomic and inline. They're stored as `{ "type": "task-ref", "attrs": { "data": { ... } } }`, with `data` kept unchanged.
|
|
39
|
+
|
|
40
|
+
- `render` is a snippet that receives `{ data, selected, editable }`.
|
|
41
|
+
- `text` is used for the plain-text clipboard and `toText`.
|
|
42
|
+
- `html` is the element other apps see when the node is copied.
|
|
43
|
+
- `schema` validates data parsed from pasted HTML.
|
|
44
|
+
- `paste` transforms pasted copies, or drops them by returning `null`.
|
|
45
|
+
|
|
46
|
+
Pass custom nodes to the editor with `nodes={[taskRef]}`.
|
|
47
|
+
|
|
48
|
+
- `editor.updateNodes(taskRef, (data, index) => ...)` changes data without moving the cursor or adding to undo history. Nodes are visited in document order, and `index` counts only nodes of that type.
|
|
49
|
+
- `editor.wrapSelection(taskRef, text => ...)` replaces the selection with a node.
|
|
50
|
+
|
|
51
|
+
## Actions
|
|
52
|
+
|
|
53
|
+
Actions appear in the menu shown over a text selection, and in the right-click menu while text is selected. With nothing selected, the browser's own menu is shown.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const actions = [
|
|
57
|
+
...formattingActions(),
|
|
58
|
+
{ i: 'list-check', text: 'Make task', run: context => context.wrapSelection(taskRef, text => createTask(text)) },
|
|
59
|
+
];
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Output
|
|
63
|
+
|
|
64
|
+
- `toHTML(content, { preset, nodes })`
|
|
65
|
+
- `toText(content, nodes)`, or `toPlainText` from `@axium/rich-text` on the server
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare module '@james-pre/locales' {
|
|
2
|
+
interface Keys {
|
|
3
|
+
'RichText.bold': {};
|
|
4
|
+
'RichText.code': {};
|
|
5
|
+
'RichText.italic': {};
|
|
6
|
+
'RichText.link': {};
|
|
7
|
+
'RichText.link_apply': {};
|
|
8
|
+
'RichText.link_placeholder': {};
|
|
9
|
+
'RichText.strike': {};
|
|
10
|
+
'RichText.suggestions_empty': {};
|
|
11
|
+
'RichText.suggestions_loading': {};
|
|
12
|
+
'RichText.task_done': {};
|
|
13
|
+
'RichText.underline': {};
|
|
14
|
+
'RichText.unlink': {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export {};
|
package/dist/.locale.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
export declare const RichTextMark: z.ZodObject<{
|
|
3
|
+
type: z.ZodString;
|
|
4
|
+
attrs: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
5
|
+
}, z.core.$strip>;
|
|
6
|
+
export interface RichTextMark extends z.infer<typeof RichTextMark> {
|
|
7
|
+
}
|
|
8
|
+
export interface RichTextNode {
|
|
9
|
+
type: string;
|
|
10
|
+
attrs?: Record<string, unknown>;
|
|
11
|
+
content?: RichTextNode[];
|
|
12
|
+
marks?: RichTextMark[];
|
|
13
|
+
text?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const RichTextNode: z.ZodType<RichTextNode>;
|
|
16
|
+
export interface RichTextDocument extends RichTextNode {
|
|
17
|
+
type: 'doc';
|
|
18
|
+
content: RichTextNode[];
|
|
19
|
+
}
|
|
20
|
+
export declare const RichTextDocument: z.ZodType<RichTextDocument>;
|
|
21
|
+
export declare function emptyDocument(): RichTextDocument;
|
|
22
|
+
/**
|
|
23
|
+
* Visit every node depth-first, parents before children.
|
|
24
|
+
* Returning `false` from `visit` skips that node's children.
|
|
25
|
+
*/
|
|
26
|
+
export declare function walk(node: RichTextNode, visit: (node: RichTextNode, parent: RichTextNode | null) => void | false): void;
|
|
27
|
+
/** Every node of the given type, depth-first. */
|
|
28
|
+
export declare function findNodes(node: RichTextNode, type: string): Generator<RichTextNode>;
|
|
29
|
+
/**
|
|
30
|
+
* Transform every descendant of `root` bottom-up, returning a new tree.
|
|
31
|
+
* `transform` receives each node with its children already transformed.
|
|
32
|
+
* It can return a replacement node, several nodes to splice in, or `null` to remove the node.
|
|
33
|
+
*/
|
|
34
|
+
export declare function mapNodes<T extends RichTextNode>(root: T, transform: (node: RichTextNode) => RichTextNode | RichTextNode[] | null): T;
|
|
35
|
+
export interface PlainTextOptions {
|
|
36
|
+
/** Text for custom inline nodes, by node type. */
|
|
37
|
+
nodes?: Record<string, (node: RichTextNode) => string>;
|
|
38
|
+
/** Inserted between block nodes. */
|
|
39
|
+
blockSeparator?: string;
|
|
40
|
+
}
|
|
41
|
+
export declare function toPlainText(node: RichTextNode, options?: PlainTextOptions): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
export const RichTextMark = z.object({
|
|
3
|
+
type: z.string(),
|
|
4
|
+
attrs: z.record(z.string(), z.unknown()).optional(),
|
|
5
|
+
});
|
|
6
|
+
export const RichTextNode = z.object({
|
|
7
|
+
type: z.string(),
|
|
8
|
+
attrs: z.record(z.string(), z.unknown()).optional(),
|
|
9
|
+
get content() {
|
|
10
|
+
return z.array(RichTextNode).optional();
|
|
11
|
+
},
|
|
12
|
+
marks: z.array(RichTextMark).optional(),
|
|
13
|
+
text: z.string().optional(),
|
|
14
|
+
});
|
|
15
|
+
export const RichTextDocument = z.object({
|
|
16
|
+
type: z.literal('doc'),
|
|
17
|
+
content: z.array(RichTextNode),
|
|
18
|
+
});
|
|
19
|
+
export function emptyDocument() {
|
|
20
|
+
return { type: 'doc', content: [{ type: 'paragraph' }] };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Visit every node depth-first, parents before children.
|
|
24
|
+
* Returning `false` from `visit` skips that node's children.
|
|
25
|
+
*/
|
|
26
|
+
export function walk(node, visit) {
|
|
27
|
+
function visitNode(current, parent) {
|
|
28
|
+
if (visit(current, parent) === false)
|
|
29
|
+
return;
|
|
30
|
+
for (const child of current.content ?? [])
|
|
31
|
+
visitNode(child, current);
|
|
32
|
+
}
|
|
33
|
+
visitNode(node, null);
|
|
34
|
+
}
|
|
35
|
+
/** Every node of the given type, depth-first. */
|
|
36
|
+
export function* findNodes(node, type) {
|
|
37
|
+
if (node.type == type)
|
|
38
|
+
yield node;
|
|
39
|
+
for (const child of node.content ?? [])
|
|
40
|
+
yield* findNodes(child, type);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Transform every descendant of `root` bottom-up, returning a new tree.
|
|
44
|
+
* `transform` receives each node with its children already transformed.
|
|
45
|
+
* It can return a replacement node, several nodes to splice in, or `null` to remove the node.
|
|
46
|
+
*/
|
|
47
|
+
export function mapNodes(root, transform) {
|
|
48
|
+
function mapChildren(node) {
|
|
49
|
+
if (!node.content)
|
|
50
|
+
return node;
|
|
51
|
+
const content = node.content.flatMap(child => transform(mapChildren(child)) ?? []);
|
|
52
|
+
return { ...node, content };
|
|
53
|
+
}
|
|
54
|
+
return mapChildren(root);
|
|
55
|
+
}
|
|
56
|
+
const inlineTypes = new Set(['text', 'hardBreak']);
|
|
57
|
+
export function toPlainText(node, options = {}) {
|
|
58
|
+
const { nodes = {}, blockSeparator = '\n' } = options;
|
|
59
|
+
function isInline(current) {
|
|
60
|
+
return inlineTypes.has(current.type) || current.type in nodes;
|
|
61
|
+
}
|
|
62
|
+
function render(current) {
|
|
63
|
+
if (current.type == 'text')
|
|
64
|
+
return current.text ?? '';
|
|
65
|
+
if (current.type == 'hardBreak')
|
|
66
|
+
return '\n';
|
|
67
|
+
if (current.type in nodes)
|
|
68
|
+
return nodes[current.type](current);
|
|
69
|
+
const children = current.content ?? [];
|
|
70
|
+
const separator = children.every(isInline) ? '' : blockSeparator;
|
|
71
|
+
return children.map(render).join(separator);
|
|
72
|
+
}
|
|
73
|
+
return render(node);
|
|
74
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<script lang="ts" generics="Data">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { CustomNodeProps } from './nodes.svelte.js';
|
|
4
|
+
|
|
5
|
+
const { render, props }: { render: Snippet<[CustomNodeProps<Data>]>; props: CustomNodeProps<Data> } = $props();
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
{@render render(props)}
|