editate 0.6.27 → 0.7.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/README.md +19 -14
- package/lib/commands.d.ts +8 -8
- package/lib/doc/node.d.ts +10 -2
- package/lib/doc/operation.d.ts +7 -10
- package/lib/doc/types-infer.d.ts +11 -13
- package/lib/editor.d.ts +6 -4
- package/lib/index.cjs +741 -718
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +731 -705
- package/lib/index.js.map +1 -1
- package/lib/plugins/blockLock.d.ts +1 -2
- package/lib/plugins/index.d.ts +1 -0
- package/lib/plugins/nodeId.d.ts +13 -0
- package/lib/plugins/singleline.d.ts +1 -1
- package/lib/queries.d.ts +10 -2
- package/package.json +32 -30
package/README.md
CHANGED
|
@@ -101,6 +101,8 @@ import { useState, useEffect, useRef, useMemo } from "react";
|
|
|
101
101
|
import {
|
|
102
102
|
createEditor,
|
|
103
103
|
plainTransferPlugin,
|
|
104
|
+
nodeIdPlugin,
|
|
105
|
+
NodeId,
|
|
104
106
|
ToggleFormat,
|
|
105
107
|
ToggleBlockAttr,
|
|
106
108
|
} from "editate";
|
|
@@ -142,7 +144,9 @@ export const App = () => {
|
|
|
142
144
|
const e = createEditor({
|
|
143
145
|
doc,
|
|
144
146
|
schema,
|
|
145
|
-
})
|
|
147
|
+
})
|
|
148
|
+
.exec(plainTransferPlugin)
|
|
149
|
+
.exec(nodeIdPlugin);
|
|
146
150
|
e.on("change", () => {
|
|
147
151
|
setDoc(e.doc);
|
|
148
152
|
});
|
|
@@ -186,11 +190,11 @@ export const App = () => {
|
|
|
186
190
|
padding: 8,
|
|
187
191
|
}}
|
|
188
192
|
>
|
|
189
|
-
{doc.children.map((b
|
|
190
|
-
<div key={
|
|
191
|
-
{b.children.map((n
|
|
193
|
+
{doc.children.map((b) => (
|
|
194
|
+
<div key={NodeId(editor, b)} style={{ textAlign: b.align }}>
|
|
195
|
+
{b.children.map((n) => (
|
|
192
196
|
<span
|
|
193
|
-
key={
|
|
197
|
+
key={NodeId(editor, n)}
|
|
194
198
|
style={{
|
|
195
199
|
fontWeight: n.bold ? "bold" : undefined,
|
|
196
200
|
fontStyle: n.italic ? "italic" : undefined,
|
|
@@ -209,15 +213,16 @@ export const App = () => {
|
|
|
209
213
|
|
|
210
214
|
### Other examples
|
|
211
215
|
|
|
212
|
-
- React ([Demo](https://inokawa.github.io/editate/react), [Source](./examples/react))
|
|
213
|
-
- Vue ([Demo](https://inokawa.github.io/editate/vue), [Source](./examples/vue))
|
|
214
|
-
- Svelte ([Demo](https://inokawa.github.io/editate/svelte), [Source](./examples/svelte))
|
|
215
|
-
- Solid ([Demo](https://inokawa.github.io/editate/solid), [Source](./examples/solid))
|
|
216
|
-
- Angular ([Demo](https://inokawa.github.io/editate/angular), [Source](./examples/angular))
|
|
217
|
-
- Preact ([Demo](https://inokawa.github.io/editate/preact), [Source](./examples/preact))
|
|
218
|
-
- Alpine ([Demo](https://inokawa.github.io/editate/alpine), [Source](./examples/alpine))
|
|
219
|
-
- Marko ([Demo](https://inokawa.github.io/editate/marko), [Source](./examples/marko))
|
|
220
|
-
- Octane ([Demo](https://inokawa.github.io/editate/octane), [Source](./examples/octane))
|
|
216
|
+
- [React](https://react.dev) ([Demo](https://inokawa.github.io/editate/react), [Source](./examples/react))
|
|
217
|
+
- [Vue](https://vuejs.org) ([Demo](https://inokawa.github.io/editate/vue), [Source](./examples/vue))
|
|
218
|
+
- [Svelte](https://svelte.dev) ([Demo](https://inokawa.github.io/editate/svelte), [Source](./examples/svelte))
|
|
219
|
+
- [Solid](https://solidjs.com) ([Demo](https://inokawa.github.io/editate/solid), [Source](./examples/solid))
|
|
220
|
+
- [Angular](https://angular.dev) ([Demo](https://inokawa.github.io/editate/angular), [Source](./examples/angular))
|
|
221
|
+
- [Preact](https://preactjs.com) ([Demo](https://inokawa.github.io/editate/preact), [Source](./examples/preact))
|
|
222
|
+
- [Alpine](https://alpinejs.dev) ([Demo](https://inokawa.github.io/editate/alpine), [Source](./examples/alpine))
|
|
223
|
+
- [Marko](https://markojs.com) ([Demo](https://inokawa.github.io/editate/marko), [Source](./examples/marko))
|
|
224
|
+
- [Octane](https://octanejs.dev) ([Demo](https://inokawa.github.io/editate/octane), [Source](./examples/octane))
|
|
225
|
+
- [Remix](https://remix.run) ([Demo](https://inokawa.github.io/editate/remix), [Source](./examples/remix))
|
|
221
226
|
- Vanilla ([Demo](https://inokawa.github.io/editate/vanilla), [Source](./examples/vanilla))
|
|
222
227
|
|
|
223
228
|
...and more! Contribution welcome!
|
package/lib/commands.d.ts
CHANGED
|
@@ -21,25 +21,25 @@ export declare function ReplaceText(editor: Editor, text: string, range?: Range)
|
|
|
21
21
|
* Replace document in the editor.
|
|
22
22
|
*/
|
|
23
23
|
export declare function ReplaceDoc<T extends DocNode>(editor: Editor<T>, fragment: T["children"]): void;
|
|
24
|
-
type ToggleableKey<T> = {
|
|
24
|
+
type ToggleableKey<T> = T extends any ? {
|
|
25
25
|
[K in keyof T]-?: T[K] extends boolean | undefined ? K : never;
|
|
26
|
-
}[keyof T];
|
|
26
|
+
}[keyof T] : never;
|
|
27
27
|
/**
|
|
28
28
|
* Format content in the selection or specified range.
|
|
29
29
|
*/
|
|
30
|
-
export declare function Format<T extends DocNode, N extends
|
|
30
|
+
export declare function Format<T extends DocNode, N extends InferTextNode<T>, K extends Exclude<N extends any ? Extract<keyof N, string> : never, "text">>(editor: Editor<T>, key: K, value: ExtractAttrValue<N, K>, range?: Range): void;
|
|
31
31
|
/**
|
|
32
32
|
* Toggle formatting in the selection or specified range.
|
|
33
33
|
*/
|
|
34
|
-
export declare function ToggleFormat<T extends DocNode>(editor: Editor<T>, key: Extract<ToggleableKey<
|
|
34
|
+
export declare function ToggleFormat<T extends DocNode>(editor: Editor<T>, key: Extract<ToggleableKey<InferTextNode<T>>, string>, range?: Range): void;
|
|
35
35
|
/**
|
|
36
|
-
* Set attr to
|
|
36
|
+
* Set attr to block nodes in the selection or specified range.
|
|
37
37
|
*/
|
|
38
|
-
export declare function SetBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, value: ExtractAttrValue<N, K>,
|
|
38
|
+
export declare function SetBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, value: ExtractAttrValue<N, K>, range?: Range): void;
|
|
39
39
|
/**
|
|
40
|
-
* Toggle attr of block
|
|
40
|
+
* Toggle attr of block nodes in the selection or specified range.
|
|
41
41
|
*/
|
|
42
|
-
export declare function ToggleBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, onValue: ExtractAttrValue<N, K>, offValue: ExtractAttrValue<N, K>,
|
|
42
|
+
export declare function ToggleBlockAttr<T extends DocNode, N extends InferLeafBlockNode<T>, K extends string>(editor: Editor<T>, key: K, onValue: ExtractAttrValue<N, K>, offValue: ExtractAttrValue<N, K>, range?: Range): void;
|
|
43
43
|
/**
|
|
44
44
|
* Set attr to a void node at the caret or specified position.
|
|
45
45
|
*/
|
package/lib/doc/node.d.ts
CHANGED
|
@@ -6,6 +6,14 @@ export declare const getLeafBlockAt: <T extends DocNode | BlockNode>(node: T, of
|
|
|
6
6
|
export declare const getLeafAt: <T extends DocNode | BlockNode>(node: T, offset: number, isBackwardAffinity?: boolean) => [node: InferInlineNode<T>, offset: number, path: Path] | null;
|
|
7
7
|
export declare const getNodeOffset: (root: DocNode | BlockNode, target: Node) => number | null;
|
|
8
8
|
export declare const offsetToPosition: (node: DocNode | BlockNode, offset: number) => DomPosition;
|
|
9
|
-
export declare function iterLeafBlocks<T extends Node>(node: T, range: Range): Generator<[
|
|
10
|
-
|
|
9
|
+
export declare function iterLeafBlocks<T extends Node>(node: T, range: Range): Generator<[
|
|
10
|
+
node: InferLeafBlockNode<T>,
|
|
11
|
+
offset: number,
|
|
12
|
+
range: Range
|
|
13
|
+
], void, void>;
|
|
14
|
+
export declare function iterLeaves<T extends Node>(node: T, range: Range): Generator<[
|
|
15
|
+
node: InferInlineNode<T>,
|
|
16
|
+
offset: number,
|
|
17
|
+
range: Range
|
|
18
|
+
], void, void>;
|
|
11
19
|
export declare const sliceText: <T extends Node>(node: T, start?: number, end?: number, voidToString?: (node: InferVoidNode<T>) => string) => string;
|
package/lib/doc/operation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Fragment,
|
|
1
|
+
import { Fragment, Range } from './types.js';
|
|
2
2
|
declare const OP_DELETE = "delete";
|
|
3
3
|
type DeleteOperation = Readonly<{
|
|
4
4
|
type: typeof OP_DELETE;
|
|
@@ -20,17 +20,14 @@ declare const OP_FORMAT = "format";
|
|
|
20
20
|
type FormatOperation = Readonly<{
|
|
21
21
|
type: typeof OP_FORMAT;
|
|
22
22
|
range: Range;
|
|
23
|
+
/** Must not be `text`, nor `children` unless the depth is 0. */
|
|
23
24
|
key: string;
|
|
24
|
-
|
|
25
|
+
/** Omit or pass undefined to remove the attr. */
|
|
26
|
+
value?: unknown;
|
|
27
|
+
/** Depth of nodes to update like array.at() on the path from the root to each leaf in the range: -1 (default) is the leaf, -2 its block and 0 the root. */
|
|
28
|
+
depth?: number;
|
|
25
29
|
}>;
|
|
26
|
-
|
|
27
|
-
type PatchNodeOperation = Readonly<{
|
|
28
|
-
type: typeof OP_PATCH_NODE;
|
|
29
|
-
path: Path;
|
|
30
|
-
key: string;
|
|
31
|
-
value: unknown;
|
|
32
|
-
}>;
|
|
33
|
-
export type Operation = DeleteOperation | InsertTextOperation | InsertNodeOperation | FormatOperation | PatchNodeOperation;
|
|
30
|
+
export type Operation = DeleteOperation | InsertTextOperation | InsertNodeOperation | FormatOperation;
|
|
34
31
|
/**
|
|
35
32
|
* Remap a position through the given operation.
|
|
36
33
|
* @param stickBefore `true` to keep the position in place when content is inserted at it, instead of moving it after the inserted content.
|
package/lib/doc/types-infer.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import { BlockNode, Node, TextNode } from './types.js';
|
|
2
|
-
|
|
3
|
-
children: readonly
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
type Parent = {
|
|
3
|
+
children: readonly Node[];
|
|
4
|
+
};
|
|
5
|
+
type Same<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false;
|
|
6
|
+
type Unseen<T, S> = T extends Parent ? true extends (S extends any ? Same<T, S> : never) ? never : T : never;
|
|
7
|
+
type Blocks<T, S> = Unseen<T, S> extends infer B extends Parent ? [B] extends [never] ? S : Blocks<B["children"][number], S | B> : never;
|
|
8
|
+
type LeafBlocks<B> = B extends Parent ? [Exclude<B["children"][number], BlockNode>] extends [never] ? never : B : never;
|
|
9
|
+
export type InferBlockNode<T extends Node> = T extends Parent ? Blocks<T["children"][number], T> : never;
|
|
10
|
+
export type InferLeafBlockNode<T extends Node> = T extends Parent ? LeafBlocks<InferBlockNode<T>> : never;
|
|
11
|
+
export type InferInlineNode<T extends Node> = T extends Parent ? Exclude<InferBlockNode<T>["children"][number], Parent> : T;
|
|
11
12
|
export type InferVoidNode<T extends Node> = Exclude<InferInlineNode<T>, TextNode>;
|
|
12
13
|
export type InferTextNode<T extends Node> = Extract<InferInlineNode<T>, TextNode>;
|
|
13
|
-
type
|
|
14
|
-
children: readonly (infer N extends Node)[];
|
|
15
|
-
} ? FlattenAllNodes<N> : never) : never;
|
|
16
|
-
export type ExtractAttrValue<N extends Node, K extends PropertyKey> = FlattenAllNodes<N> extends infer AllNodes extends Node ? AllNodes extends any ? K extends keyof AllNodes ? AllNodes[K] : never : never : never;
|
|
14
|
+
export type ExtractAttrValue<N extends Node, K extends PropertyKey> = N extends any ? K extends keyof N ? N[K] : BlockNode extends N ? any : never : never;
|
|
17
15
|
export {};
|
package/lib/editor.d.ts
CHANGED
|
@@ -61,10 +61,12 @@ export type CopyHook = (dataTransfer: DataTransfer) => void;
|
|
|
61
61
|
export type PasteHook = (dataTransfer: DataTransfer) => string | Fragment | true | null;
|
|
62
62
|
type EditorHookMap = {
|
|
63
63
|
/**
|
|
64
|
-
* Call `next(op)` to continue applying the operation, or `next()` with a nullish value to cancel it.
|
|
64
|
+
* Call `next(op)` to continue applying the operation, or `next()` with a nullish value to cancel it with its transaction.
|
|
65
|
+
* `next` returns the operation applied to the document after the following hooks, or null if it was cancelled or failed to apply. The transaction may still be discarded afterwards.
|
|
66
|
+
*
|
|
65
67
|
* If the hook returns without calling `next`, the operation is passed through as is.
|
|
66
68
|
*/
|
|
67
|
-
apply: (op: Operation, next: (op?: Operation | null) =>
|
|
69
|
+
apply: (op: Operation, next: (op?: Operation | null) => Operation | null) => void;
|
|
68
70
|
mount: (element: HTMLElement, parser: Parser) => void | (() => void);
|
|
69
71
|
keyboard: KeyboardHook;
|
|
70
72
|
copy: CopyHook;
|
|
@@ -82,10 +84,10 @@ export interface Editor<T extends DocNode = DocNode> {
|
|
|
82
84
|
*/
|
|
83
85
|
readonly: boolean;
|
|
84
86
|
/**
|
|
85
|
-
*
|
|
87
|
+
* Applies editing operations as a transaction, which is discarded if one of them is cancelled or throws, or the result is rejected by the schema.
|
|
86
88
|
* @param op {@link Operation}
|
|
87
89
|
*/
|
|
88
|
-
apply(op: Operation | Operation[]): this;
|
|
90
|
+
apply(op: Operation | readonly Operation[]): this;
|
|
89
91
|
/**
|
|
90
92
|
* Executes a function with editor bound as context.
|
|
91
93
|
* @param fn {@link EditorCommandOrPlugin} or {@link EditorQuery}
|