From ff15f2fa7007f46664f513a4084f580f8f03c67d Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 13 May 2022 16:30:23 +0900 Subject: [PATCH 1/4] :boom::fix: Get both Cursor and Selection at once --- browser/dom/cursor.ts | 13 ------------- browser/dom/selection.ts | 13 ------------- browser/dom/stores.ts | 21 ++++++++++++++++++--- 3 files changed, 18 insertions(+), 29 deletions(-) delete mode 100644 browser/dom/cursor.ts delete mode 100644 browser/dom/selection.ts diff --git a/browser/dom/cursor.ts b/browser/dom/cursor.ts deleted file mode 100644 index 5507f1f..0000000 --- a/browser/dom/cursor.ts +++ /dev/null @@ -1,13 +0,0 @@ -/// -/// -/// - -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.'); -}; diff --git a/browser/dom/selection.ts b/browser/dom/selection.ts deleted file mode 100644 index 691c21d..0000000 --- a/browser/dom/selection.ts +++ /dev/null @@ -1,13 +0,0 @@ -/// -/// -/// - -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.'); -}; 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 { From ea69d2744b275f91f1d9726133e117e8270a9d07 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 13 May 2022 16:34:11 +0900 Subject: [PATCH 2/4] :sparkles: Get both Cursor and Selection at once --- browser/dom/cursor.ts | 7 +------ browser/dom/mod.ts | 1 + browser/dom/selection.ts | 7 +------ browser/dom/stores.ts | 21 ++++++++++++++++++--- 4 files changed, 21 insertions(+), 15 deletions(-) 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 { From 4764ef82f261cb3eac573cd095b351a91f297dc2 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 13 May 2022 16:37:04 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:bug:=20=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=81=97=E5=BF=98=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/selection.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 browser/dom/selection.ts diff --git a/browser/dom/selection.ts b/browser/dom/selection.ts new file mode 100644 index 0000000..915af38 --- /dev/null +++ b/browser/dom/selection.ts @@ -0,0 +1,8 @@ +/// +/// +/// + +import { takeStores } from "./stores.ts"; +import { Selection } from "./selection.d.ts"; + +export const takeSelection = (): Selection => takeStores().selection; From d9bbeaa35c144079431e9608f1dc86d9e7fe672d Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 13 May 2022 16:40:12 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:bug:=20=E3=81=BE=E3=81=A0=E3=83=87?= =?UTF-8?q?=E3=82=B0=E3=83=AC=E3=81=A6=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/cursor.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 browser/dom/cursor.ts diff --git a/browser/dom/cursor.ts b/browser/dom/cursor.ts new file mode 100644 index 0000000..d4c4721 --- /dev/null +++ b/browser/dom/cursor.ts @@ -0,0 +1,8 @@ +/// +/// +/// + +import { takeStores } from "./stores.ts"; +import { Cursor } from "./cursor.d.ts"; + +export const takeCursor = (): Cursor => takeStores().cursor;