Skip to content

✨ Provide a shallow copy of lines #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2023
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
1 change: 1 addition & 0 deletions browser/dom/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export * from "./cache.ts";
export * from "./cursor.ts";
export * from "./selection.ts";
export * from "./stores.ts";
export * from "./takeInternalLines.ts";
export * from "./pushPageTransition.ts";
export * from "./extractCodeFiles.ts";
41 changes: 41 additions & 0 deletions browser/dom/takeInternalLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { lines } from "./dom.ts";
import { BaseLine } from "../../deps/scrapbox.ts";

/** Scrapbox内部の本文データの参照を取得する
*
* `scrapbox.Page.lines`はdeep cloneされてしまうので、performanceの問題が発生する場合がある
*
* なるべくデータのcloneを発生させたくない場合にこちらを使う
*
* 注意
* - 参照をそのまま返しているだけなので、中身を書き換えられてしまう。型定義で変更不能にはしてあるが、JS経由ならいくらでも操作できる
* - `scrapbox.Page.lines`とは違って構文解析情報が付与されない
*/
export const takeInternalLines = (): readonly BaseLine[] => {
const linesEl = lines();
if (!linesEl) {
throw Error(`div.lines is not found.`);
}

const reactKey = Object.keys(linesEl)
.find((key) => key.startsWith("__reactFiber"));
if (!reactKey) {
throw Error(
'div.lines must has the property whose name starts with "__reactFiber"',
);
}

// @ts-ignore DOMを無理矢理objectとして扱っている
return (linesEl[reactKey] as ReactFiber).return.stateNode.props
.lines as const;
};

interface ReactFiber {
return: {
stateNode: {
props: {
lines: BaseLine[];
};
};
};
}