editate 0.5.0 → 0.6.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/README.md CHANGED
@@ -96,7 +96,12 @@ You can define document schema with [Standard Schema](https://github.com/standar
96
96
 
97
97
  ```tsx
98
98
  import { useState, useEffect, useRef, useMemo } from "react";
99
- import { createEditor, ToggleFormat, ToggleBlockAttr } from "editate";
99
+ import {
100
+ createEditor,
101
+ plainTransferPlugin,
102
+ ToggleFormat,
103
+ ToggleBlockAttr,
104
+ } from "editate";
100
105
  import * as z from "zod";
101
106
 
102
107
  const schema = z.strictObject({
@@ -131,15 +136,16 @@ export const App = () => {
131
136
  ],
132
137
  });
133
138
 
134
- const editor = useMemo(
135
- () =>
136
- createEditor({
137
- doc,
138
- schema,
139
- onChange: setDoc,
140
- }),
141
- [],
142
- );
139
+ const editor = useMemo(() => {
140
+ const e = createEditor({
141
+ doc,
142
+ schema,
143
+ }).exec(plainTransferPlugin);
144
+ e.on("change", () => {
145
+ setDoc(e.doc);
146
+ });
147
+ return e;
148
+ }, []);
143
149
 
144
150
  useEffect(() => {
145
151
  return editor.input(ref.current);
package/lib/doc/edit.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import type { DocNode, Fragment, Path, BlockNode, Node, DomPosition } from "./types.js";
1
+ import type { DocNode, Fragment, Path, BlockNode, Node, DomPosition, Range } from "./types.js";
2
2
  declare const OP_DELETE = "delete";
3
3
  type DeleteOperation = Readonly<{
4
4
  type: typeof OP_DELETE;
5
- start: number;
6
- end: number;
5
+ range: Range;
7
6
  }>;
8
7
  declare const OP_INSERT_TEXT = "insert_text";
9
8
  type InsertTextOperation = Readonly<{
@@ -20,8 +19,7 @@ type InsertNodeOperation = Readonly<{
20
19
  declare const OP_SET_ATTR = "set_attr";
21
20
  type SetAttrOperation = Readonly<{
22
21
  type: typeof OP_SET_ATTR;
23
- start: number;
24
- end: number;
22
+ range: Range;
25
23
  key: string;
26
24
  value: unknown;
27
25
  }>;
@@ -33,17 +31,7 @@ type SetNodeAttrOperation = Readonly<{
33
31
  value: unknown;
34
32
  }>;
35
33
  export type Operation = DeleteOperation | InsertTextOperation | InsertNodeOperation | SetAttrOperation | SetNodeAttrOperation;
36
- export declare class Transaction {
37
- private readonly _ops;
38
- constructor(ops?: readonly Operation[]);
39
- get ops(): readonly Operation[];
40
- insertText(at: number, text: string): this;
41
- insertFragment(at: number, fragment: Fragment): this;
42
- delete(start: number, end: number): this;
43
- format(start: number, end: number, key: string, value: unknown): this;
44
- attr(at: Path, key: string, value: unknown): this;
45
- transform(position: number): number;
46
- }
47
34
  export declare const getNodeSize: (node: Node) => number;
48
35
  export declare const offsetToPosition: (node: DocNode | BlockNode, offset: number) => DomPosition;
36
+ export declare const rebase: (position: number, ops: readonly Operation[]) => number;
49
37
  export {};
package/lib/editor.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- import type { DocNode, Selection } from "./doc/types.js";
3
- import { Transaction, type Operation } from "./doc/edit.js";
4
- import { type CopyHook, type PasteHook, type KeyboardHook } from "./hooks/index.js";
2
+ import type { DocNode, Fragment, Selection } from "./doc/types.js";
3
+ import { type Operation } from "./doc/edit.js";
4
+ import type { Parser } from "./dom/parser.js";
5
5
  type EditorCommand<A extends unknown[], T extends DocNode = DocNode> = (editor: Editor<T>, ...args: A) => void | undefined;
6
6
  type EditorQuery<A extends unknown[], V, T extends DocNode = DocNode> = (editor: Editor<T>, ...args: A) => V;
7
7
  /**
@@ -20,30 +20,10 @@ export interface EditorOptions<T extends DocNode, S extends StandardSchemaV1<T,
20
20
  * The state editable or not.
21
21
  */
22
22
  readonly?: boolean;
23
- /**
24
- * Functions to handle keyboard events.
25
- *
26
- * Return `true` if you want to stop propagation.
27
- */
28
- keyboard?: KeyboardHook[];
29
- /**
30
- * Functions to handle copy events
31
- * @default [plainCopy()]
32
- */
33
- copy?: [CopyHook, ...rest: CopyHook[]];
34
- /**
35
- * Functions to handle paste / drop events
36
- * @default [plainPaste()]
37
- */
38
- paste?: [PasteHook, ...rest: PasteHook[]];
39
23
  /**
40
24
  * TODO
41
25
  */
42
26
  isBlock?: (node: HTMLElement) => boolean;
43
- /**
44
- * Callback invoked when document changes.
45
- */
46
- onChange: (doc: T) => void;
47
27
  /**
48
28
  * Callback invoked when errors happen.
49
29
  *
@@ -56,10 +36,26 @@ type EditorEventMap = {
56
36
  selectionchange: () => void;
57
37
  readonly: () => void;
58
38
  };
39
+ /**
40
+ * Functions to handle keyboard events.
41
+ *
42
+ * Return `true` if you want to stop propagation.
43
+ */
44
+ export type KeyboardHook = (keyboard: KeyboardEvent) => boolean | void;
45
+ /**
46
+ * Functions to handle copy events
47
+ */
48
+ export type CopyHook = (dataTransfer: DataTransfer, doc: Fragment, element: Element) => void;
49
+ /**
50
+ * Functions to handle paste / drop events
51
+ */
52
+ export type PasteHook = (dataTransfer: DataTransfer, parser: Parser) => string | Fragment | null;
59
53
  type EditorHookMap = {
60
54
  apply: (op: Operation, next: (op?: Operation) => void) => void;
61
55
  mount: (element: HTMLElement) => void | (() => void);
62
56
  keyboard: KeyboardHook;
57
+ copy: CopyHook;
58
+ paste: PasteHook;
63
59
  };
64
60
  /**
65
61
  * The editor instance.
@@ -74,9 +70,9 @@ export interface Editor<T extends DocNode = DocNode> {
74
70
  readonly: boolean;
75
71
  /**
76
72
  * Dispatches editing operations.
77
- * @param tr {@link Transaction}
73
+ * @param op {@link Operation}
78
74
  */
79
- apply(tr: Transaction): this;
75
+ apply(op: Operation | Operation[]): this;
80
76
  /**
81
77
  * Executes a function with editor bound as context.
82
78
  * @param fn {@link EditorCommand} or {@link EditorQuery}
@@ -103,5 +99,5 @@ export interface Editor<T extends DocNode = DocNode> {
103
99
  /**
104
100
  * A function to initialize {@link Editor}.
105
101
  */
106
- export declare const createEditor: <T extends DocNode, S extends StandardSchemaV1<T, T> | void = void>({ doc, readonly, schema, keyboard, copy: copyHooks, paste: pasteHooks, isBlock, onChange, onError, }: EditorOptions<T, S>) => Editor<T>;
102
+ export declare const createEditor: <T extends DocNode, S extends StandardSchemaV1<T, T> | void = void>({ doc, readonly, schema, isBlock, onError, }: EditorOptions<T, S>) => Editor<T>;
107
103
  export {};