diff --git a/browser/dom/cursor.ts b/browser/dom/cursor.ts index 5507f1f..d4c4721 100644 --- a/browser/dom/cursor.ts +++ b/browser/dom/cursor.ts @@ -5,9 +5,4 @@ import { takeStores } from "./stores.ts"; import { Cursor } from "./cursor.d.ts"; -export const takeCursor = (): Cursor => { - for (const store of takeStores()) { - if ("goByAction" in store) return store; - } - throw Error('#text-input must has a "Cursor" store.'); -}; +export const takeCursor = (): Cursor => takeStores().cursor; diff --git a/browser/dom/mod.ts b/browser/dom/mod.ts index 9134e01..b046f36 100644 --- a/browser/dom/mod.ts +++ b/browser/dom/mod.ts @@ -10,3 +10,4 @@ export * from "./open.ts"; export * from "./cache.ts"; export * from "./cursor.ts"; export * from "./selection.ts"; +export * from "./stores.ts"; diff --git a/browser/dom/selection.ts b/browser/dom/selection.ts index 691c21d..915af38 100644 --- a/browser/dom/selection.ts +++ b/browser/dom/selection.ts @@ -5,9 +5,4 @@ import { takeStores } from "./stores.ts"; import { Selection } from "./selection.d.ts"; -export const takeSelection = (): Selection => { - for (const store of takeStores()) { - if ("hasSelection" in store) return store; - } - throw Error('#text-input must has a "Selection" store.'); -}; +export const takeSelection = (): Selection => takeStores().selection; diff --git a/browser/dom/stores.ts b/browser/dom/stores.ts index 99d536f..c9572eb 100644 --- a/browser/dom/stores.ts +++ b/browser/dom/stores.ts @@ -6,7 +6,7 @@ import { textInput } from "./dom.ts"; import { Cursor } from "./cursor.d.ts"; import { Selection } from "./selection.d.ts"; -export const takeStores = (): (Cursor | Selection)[] => { +export const takeStores = (): { cursor: Cursor; selection: Selection } => { const textarea = textInput(); if (!textarea) { throw Error(`#text-input is not found.`); @@ -21,9 +21,24 @@ export const takeStores = (): (Cursor | Selection)[] => { } // @ts-ignore DOMを無理矢理objectとして扱っている - return (textarea[ + const stores = (textarea[ reactKey - ] as ReactFiber).return.return.stateNode._stores; + ] as ReactFiber).return.return.stateNode._stores as (Cursor | Selection)[]; + + const cursor = stores.find((store) => + store.constructor.name === "Cursor" + ) as (Cursor | undefined); + if (!cursor) { + throw Error('#text-input must has a "Cursor" store.'); + } + const selection = stores.find((store) => + store.constructor.name === "Selection" + ) as (Selection | undefined); + if (!selection) { + throw Error('#text-input must has a "Selection" store.'); + } + + return { cursor, selection }; }; interface ReactFiber {