Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions browser/dom/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions browser/dom/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./open.ts";
export * from "./cache.ts";
export * from "./cursor.ts";
export * from "./selection.ts";
export * from "./stores.ts";
7 changes: 1 addition & 6 deletions browser/dom/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 18 additions & 3 deletions browser/dom/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand All @@ -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 {
Expand Down