File tree Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Expand file tree Collapse file tree 4 files changed +21
-15
lines changed Original file line number Diff line number Diff line change 55import { takeStores } from "./stores.ts" ;
66import { Cursor } from "./cursor.d.ts" ;
77
8- export const takeCursor = ( ) : Cursor => {
9- for ( const store of takeStores ( ) ) {
10- if ( "goByAction" in store ) return store ;
11- }
12- throw Error ( '#text-input must has a "Cursor" store.' ) ;
13- } ;
8+ export const takeCursor = ( ) : Cursor => takeStores ( ) . cursor ;
Original file line number Diff line number Diff line change @@ -10,3 +10,4 @@ export * from "./open.ts";
1010export * from "./cache.ts" ;
1111export * from "./cursor.ts" ;
1212export * from "./selection.ts" ;
13+ export * from "./stores.ts" ;
Original file line number Diff line number Diff line change 55import { takeStores } from "./stores.ts" ;
66import { Selection } from "./selection.d.ts" ;
77
8- export const takeSelection = ( ) : Selection => {
9- for ( const store of takeStores ( ) ) {
10- if ( "hasSelection" in store ) return store ;
11- }
12- throw Error ( '#text-input must has a "Selection" store.' ) ;
13- } ;
8+ export const takeSelection = ( ) : Selection => takeStores ( ) . selection ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { textInput } from "./dom.ts";
66import { Cursor } from "./cursor.d.ts" ;
77import { Selection } from "./selection.d.ts" ;
88
9- export const takeStores = ( ) : ( Cursor | Selection ) [ ] => {
9+ export const takeStores = ( ) : { cursor : Cursor ; selection : Selection } => {
1010 const textarea = textInput ( ) ;
1111 if ( ! textarea ) {
1212 throw Error ( `#text-input is not found.` ) ;
@@ -21,9 +21,24 @@ export const takeStores = (): (Cursor | Selection)[] => {
2121 }
2222
2323 // @ts -ignore DOMを無理矢理objectとして扱っている
24- return ( textarea [
24+ const stores = ( textarea [
2525 reactKey
26- ] as ReactFiber ) . return . return . stateNode . _stores ;
26+ ] as ReactFiber ) . return . return . stateNode . _stores as ( Cursor | Selection ) [ ] ;
27+
28+ const cursor = stores . find ( ( store ) =>
29+ store . constructor . name === "Cursor"
30+ ) as ( Cursor | undefined ) ;
31+ if ( ! cursor ) {
32+ throw Error ( '#text-input must has a "Cursor" store.' ) ;
33+ }
34+ const selection = stores . find ( ( store ) =>
35+ store . constructor . name === "Selection"
36+ ) as ( Selection | undefined ) ;
37+ if ( ! selection ) {
38+ throw Error ( '#text-input must has a "Selection" store.' ) ;
39+ }
40+
41+ return { cursor, selection } ;
2742} ;
2843
2944interface ReactFiber {
You can’t perform that action at this time.
0 commit comments