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 5
5
import { takeStores } from "./stores.ts" ;
6
6
import { Cursor } from "./cursor.d.ts" ;
7
7
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";
10
10
export * from "./cache.ts" ;
11
11
export * from "./cursor.ts" ;
12
12
export * from "./selection.ts" ;
13
+ export * from "./stores.ts" ;
Original file line number Diff line number Diff line change 5
5
import { takeStores } from "./stores.ts" ;
6
6
import { Selection } from "./selection.d.ts" ;
7
7
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";
6
6
import { Cursor } from "./cursor.d.ts" ;
7
7
import { Selection } from "./selection.d.ts" ;
8
8
9
- export const takeStores = ( ) : ( Cursor | Selection ) [ ] => {
9
+ export const takeStores = ( ) : { cursor : Cursor ; selection : Selection } => {
10
10
const textarea = textInput ( ) ;
11
11
if ( ! textarea ) {
12
12
throw Error ( `#text-input is not found.` ) ;
@@ -21,9 +21,24 @@ export const takeStores = (): (Cursor | Selection)[] => {
21
21
}
22
22
23
23
// @ts -ignore DOMを無理矢理objectとして扱っている
24
- return ( textarea [
24
+ const stores = ( textarea [
25
25
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 } ;
27
42
} ;
28
43
29
44
interface ReactFiber {
You can’t perform that action at this time.
0 commit comments