Skip to content

Commit 5aae108

Browse files
authored
Merge pull request #118 from takker99/take-internal-lines
✨ Provide a shallow copy of `lines`
2 parents c8ced06 + 3701c05 commit 5aae108

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

browser/dom/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export * from "./cache.ts";
1111
export * from "./cursor.ts";
1212
export * from "./selection.ts";
1313
export * from "./stores.ts";
14+
export * from "./takeInternalLines.ts";
1415
export * from "./pushPageTransition.ts";
1516
export * from "./extractCodeFiles.ts";

browser/dom/takeInternalLines.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { lines } from "./dom.ts";
2+
import { BaseLine } from "../../deps/scrapbox.ts";
3+
4+
/** Scrapbox内部の本文データの参照を取得する
5+
*
6+
* `scrapbox.Page.lines`はdeep cloneされてしまうので、performanceの問題が発生する場合がある
7+
*
8+
* なるべくデータのcloneを発生させたくない場合にこちらを使う
9+
*
10+
* 注意
11+
* - 参照をそのまま返しているだけなので、中身を書き換えられてしまう。型定義で変更不能にはしてあるが、JS経由ならいくらでも操作できる
12+
* - `scrapbox.Page.lines`とは違って構文解析情報が付与されない
13+
*/
14+
export const takeInternalLines = (): readonly BaseLine[] => {
15+
const linesEl = lines();
16+
if (!linesEl) {
17+
throw Error(`div.lines is not found.`);
18+
}
19+
20+
const reactKey = Object.keys(linesEl)
21+
.find((key) => key.startsWith("__reactFiber"));
22+
if (!reactKey) {
23+
throw Error(
24+
'div.lines must has the property whose name starts with "__reactFiber"',
25+
);
26+
}
27+
28+
// @ts-ignore DOMを無理矢理objectとして扱っている
29+
return (linesEl[reactKey] as ReactFiber).return.stateNode.props
30+
.lines as const;
31+
};
32+
33+
interface ReactFiber {
34+
return: {
35+
stateNode: {
36+
props: {
37+
lines: BaseLine[];
38+
};
39+
};
40+
};
41+
}

0 commit comments

Comments
 (0)