Skip to content

Commit ed1e90b

Browse files
authored
Merge pull request #53 from takker99/take-all-stores
✨ Get both Cursor and Selection at once
2 parents 2f64c98 + d9bbeaa commit ed1e90b

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

browser/dom/cursor.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,4 @@
55
import { takeStores } from "./stores.ts";
66
import { 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;

browser/dom/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from "./open.ts";
1010
export * from "./cache.ts";
1111
export * from "./cursor.ts";
1212
export * from "./selection.ts";
13+
export * from "./stores.ts";

browser/dom/selection.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,4 @@
55
import { takeStores } from "./stores.ts";
66
import { 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;

browser/dom/stores.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { textInput } from "./dom.ts";
66
import { Cursor } from "./cursor.d.ts";
77
import { 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

2944
interface ReactFiber {

0 commit comments

Comments
 (0)