File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -11,5 +11,6 @@ export * from "./cache.ts";
11
11
export * from "./cursor.ts" ;
12
12
export * from "./selection.ts" ;
13
13
export * from "./stores.ts" ;
14
+ export * from "./takeInternalLines.ts" ;
14
15
export * from "./pushPageTransition.ts" ;
15
16
export * from "./extractCodeFiles.ts" ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments