4
4
import { isNone , isNumber , isString } from "../../is.ts" ;
5
5
import { ensureArray } from "../../ensure.ts" ;
6
6
import { getCachedLines } from "./getCachedLines.ts" ;
7
- import type { Line , Scrapbox } from "../../deps/scrapbox.ts" ;
7
+ import { takeInternalLines } from "./takeInternalLines.ts" ;
8
+ import type { BaseLine , Line } from "../../deps/scrapbox.ts" ;
8
9
import { lines } from "./dom.ts" ;
9
10
import * as Text from "../../text.ts" ;
10
- declare const scrapbox : Scrapbox ;
11
11
12
12
/** Get the line id from value
13
13
*
@@ -21,9 +21,7 @@ export const getLineId = <T extends HTMLElement>(
21
21
if ( isNone ( value ) ) return undefined ;
22
22
23
23
// 行番号のとき
24
- if ( isNumber ( value ) ) {
25
- return getLine ( value ) ?. id ;
26
- }
24
+ if ( isNumber ( value ) ) return getBaseLine ( value ) ?. id ;
27
25
// 行IDのとき
28
26
if ( isString ( value ) ) return value . startsWith ( "L" ) ? value . slice ( 1 ) : value ;
29
27
@@ -51,7 +49,7 @@ export const getLineNo = <T extends HTMLElement>(
51
49
if ( isNumber ( value ) ) return value ;
52
50
// 行ID or DOMのとき
53
51
const id = getLineId ( value ) ;
54
- return id ? getLines ( ) . findIndex ( ( line ) => line . id === id ) : - 1 ;
52
+ return id ? takeInternalLines ( ) . findIndex ( ( line ) => line . id === id ) : - 1 ;
55
53
} ;
56
54
57
55
export const getLine = < T extends HTMLElement > (
@@ -60,14 +58,24 @@ export const getLine = <T extends HTMLElement>(
60
58
if ( isNone ( value ) ) return undefined ;
61
59
62
60
// 行番号のとき
63
- if ( isNumber ( value ) ) {
64
- return getLines ( ) [ value ] ;
65
- }
61
+ if ( isNumber ( value ) ) return getLines ( ) [ value ] ;
66
62
// 行ID or DOMのとき
67
63
const id = getLineId ( value ) ;
68
64
return id ? getLines ( ) . find ( ( line ) => line . id === id ) : undefined ;
69
65
} ;
70
66
67
+ export const getBaseLine = < T extends HTMLElement > (
68
+ value ?: number | string | T ,
69
+ ) : BaseLine | undefined => {
70
+ if ( isNone ( value ) ) return undefined ;
71
+
72
+ // 行番号のとき
73
+ if ( isNumber ( value ) ) return takeInternalLines ( ) [ value ] ;
74
+ // 行ID or DOMのとき
75
+ const id = getLineId ( value ) ;
76
+ return id ? takeInternalLines ( ) . find ( ( line ) => line . id === id ) : undefined ;
77
+ } ;
78
+
71
79
export const getLineDOM = < T extends HTMLElement > (
72
80
value ?: number | string | T ,
73
81
) : HTMLDivElement | undefined => {
@@ -82,7 +90,7 @@ export const getLineDOM = <T extends HTMLElement>(
82
90
export const isLineDOM = ( dom : unknown ) : dom is HTMLDivElement =>
83
91
dom instanceof HTMLDivElement && dom . classList . contains ( "line" ) ;
84
92
85
- export const getLineCount = ( ) : number => getLines ( ) . length ;
93
+ export const getLineCount = ( ) : number => takeInternalLines ( ) . length ;
86
94
87
95
export const getLines = ( ) : readonly Line [ ] => {
88
96
const lines = getCachedLines ( ) ;
@@ -96,9 +104,9 @@ export const getText = <T extends HTMLElement>(
96
104
if ( isNone ( value ) ) return undefined ;
97
105
98
106
// 数字と文字列は行として扱う
99
- if ( isNumber ( value ) || isString ( value ) ) return getLine ( value ) ?. text ;
107
+ if ( isNumber ( value ) || isString ( value ) ) return getBaseLine ( value ) ?. text ;
100
108
if ( ! ( value instanceof HTMLElement ) ) return ;
101
- if ( isLineDOM ( value ) ) return getLine ( value ) ?. text ;
109
+ if ( isLineDOM ( value ) ) return getBaseLine ( value ) ?. text ;
102
110
// 文字のDOMだったとき
103
111
if ( value . classList . contains ( "char-index" ) ) {
104
112
return value . textContent ?? undefined ;
@@ -108,11 +116,11 @@ export const getText = <T extends HTMLElement>(
108
116
value . classList . contains ( "line" ) ||
109
117
value . getElementsByClassName ( "lines" ) ?. [ 0 ]
110
118
) {
111
- return getLines ( ) . map ( ( { text } ) => text ) . join ( "\n" ) ;
119
+ return takeInternalLines ( ) . map ( ( { text } ) => text ) . join ( "\n" ) ;
112
120
}
113
121
//中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す
114
122
const chars = [ ] as number [ ] ;
115
- const line = getLine ( value ) ;
123
+ const line = getBaseLine ( value ) ;
116
124
if ( isNone ( line ) ) return ;
117
125
for ( const dom of getChars ( value ) ) {
118
126
chars . push ( getIndex ( dom ) ) ;
0 commit comments