99 serializeRange ,
1010} from "./dom/index.js" ;
1111import { createMutationObserver } from "./mutation.js" ;
12- import type { Doc , Fragment , SelectionSnapshot } from "./doc/types.js" ;
12+ import type { DocBase , Fragment , SelectionSnapshot } from "./doc/types.js" ;
1313import { isString , microtask } from "./utils.js" ;
1414import type { EditorCommand } from "./commands.js" ;
1515import {
@@ -19,7 +19,6 @@ import {
1919 isDocEqual ,
2020} from "./doc/edit.js" ;
2121import { singlelinePlugin } from "./plugins/singleline.js" ;
22- import type { DocSchema } from "./schema/index.js" ;
2322import type { ParserConfig } from "./dom/parser.js" ;
2423import { comparePosition , toRange } from "./doc/position.js" ;
2524import type { PluginObject } from "./plugins/types.js" ;
@@ -95,15 +94,15 @@ type KeydownCallback = (keyboard: KeyboardPayload) => boolean | void;
9594/**
9695 * Options of {@link createEditor}.
9796 */
98- export interface EditorOptions < T > {
99- /**
100- * TODO
101- */
102- schema : DocSchema < T > ;
97+ export interface EditorOptions < T extends DocBase > {
10398 /**
10499 * Initial document state.
105100 */
106101 doc : T ;
102+ /**
103+ * TODO
104+ */
105+ singleline ?: boolean ;
107106 /**
108107 * Functions to handle copy events
109108 * @default [plainCopy()]
@@ -140,7 +139,7 @@ export interface EditorOptions<T> {
140139 * Methods of editor instance.
141140 */
142141export interface Editor {
143- readonly doc : Doc ;
142+ readonly doc : DocBase ;
144143 readonly selection : SelectionSnapshot ;
145144 /**
146145 * The getter/setter for the editor's read-only state.
@@ -164,9 +163,9 @@ export interface Editor {
164163/**
165164 * A function to initialize editor.
166165 */
167- export const createEditor = < T > ( {
168- schema : { single : isSingleline , js : docToJS , doc : jsToDoc } ,
166+ export const createEditor = < T extends DocBase > ( {
169167 doc : initialDoc ,
168+ singleline : isSingleline ,
170169 copy : copyExtensions = [ plainCopy ( ) ] ,
171170 paste : pasteExtensions = [ plainPaste ( ) ] ,
172171 isBlock = defaultIsBlockNode ,
@@ -178,11 +177,11 @@ export const createEditor = <T>({
178177 let readonly = false ;
179178 let setContentEditable : ( ) => void = noop ;
180179
181- const doc = ( ) : Doc => history . get ( ) [ 0 ] ;
180+ const doc = ( ) : T => history . get ( ) [ 0 ] ;
182181
183182 const history = createHistory <
184- readonly [ doc : Doc , selection : SelectionSnapshot ]
185- > ( [ jsToDoc ( initialDoc ) , selection ] ) ;
183+ readonly [ doc : T , selection : SelectionSnapshot ]
184+ > ( [ initialDoc , selection ] ) ;
186185
187186 const plugins : PluginObject [ ] = [ ] ;
188187 if ( isSingleline ) {
@@ -196,7 +195,7 @@ export const createEditor = <T>({
196195 const nextHistory = e . shiftKey ? history . redo ( ) : history . undo ( ) ;
197196
198197 if ( nextHistory ) {
199- onChange ( docToJS ( nextHistory [ 0 ] ) ) ;
198+ onChange ( nextHistory [ 0 ] ) ;
200199 selection = nextHistory [ 1 ] ;
201200 }
202201 return true ;
@@ -209,13 +208,17 @@ export const createEditor = <T>({
209208 keydownHandlers . push ( onKeyDownHandler ) ;
210209 }
211210
212- const applyTransaction = plugins . reduceRight (
211+ const applyTransaction = plugins . reduceRight <
212+ (
213+ doc : T ,
214+ sel : SelectionSnapshot ,
215+ tr : Transaction ,
216+ ) => ReturnType < typeof _applyTransaction >
217+ > (
213218 ( acc , { apply : fn } ) => {
214219 return fn ? ( doc , sel , tr ) => fn ( ( tr ) => acc ( doc , sel , tr ) , tr ) : acc ;
215220 } ,
216- ( doc : Doc , sel : SelectionSnapshot , tr : Transaction ) => {
217- return _applyTransaction ( doc , sel , tr , onError ) ;
218- } ,
221+ ( doc , sel , tr ) => _applyTransaction ( doc , sel , tr , onError ) ,
219222 ) ;
220223
221224 const transactions : Transaction [ ] = [ ] ;
@@ -241,14 +244,14 @@ export const createEditor = <T>({
241244
242245 const commit = ( ) => {
243246 if ( transactions . length ) {
244- let nextDoc : Doc = doc ( ) ;
247+ let nextDoc : T = doc ( ) ;
245248 let nextSelection : SelectionSnapshot = selection ;
246249
247250 let tr : Transaction | undefined ;
248251 while ( ( tr = transactions . pop ( ) ) ) {
249252 const res = applyTransaction ( nextDoc , nextSelection , tr ) ;
250253 if ( res ) {
251- nextDoc = res [ 0 ] ;
254+ nextDoc = res [ 0 ] as T ; // TODO improve
252255 nextSelection = res [ 1 ] ;
253256 }
254257 }
@@ -258,7 +261,7 @@ export const createEditor = <T>({
258261 if ( ! isDocEqual ( nextDoc , currentDoc ) ) {
259262 history . set ( [ currentDoc , selection ] ) ;
260263 history . push ( [ nextDoc , nextSelection ] ) ;
261- onChange ( docToJS ( nextDoc ) ) ;
264+ onChange ( nextDoc ) ;
262265 }
263266
264267 selection = nextSelection ;
0 commit comments