1
- import { isNone , isNumber , isString } from "../../is.ts " ;
2
- import { ensureArray } from "../../ensure.ts " ;
1
+ import { isNumber , isString , isUndefined } from "@core/unknownutil " ;
2
+ import { ensure , isArray } from "@core/unknownutil " ;
3
3
import { getCachedLines } from "./getCachedLines.ts" ;
4
4
import { takeInternalLines } from "./takeInternalLines.ts" ;
5
- import type { BaseLine , Line } from "../../deps/scrapbox.ts " ;
5
+ import type { BaseLine , Line } from "@cosense/types/userscript " ;
6
6
import { lines } from "./dom.ts" ;
7
7
import * as Text from "../../text.ts" ;
8
8
@@ -15,7 +15,7 @@ import * as Text from "../../text.ts";
15
15
export const getLineId = < T extends HTMLElement > (
16
16
value ?: number | string | T ,
17
17
) : string | undefined => {
18
- if ( isNone ( value ) ) return undefined ;
18
+ if ( isUndefined ( value ) ) return undefined ;
19
19
20
20
// 行番号のとき
21
21
if ( isNumber ( value ) ) return getBaseLine ( value ) ?. id ;
@@ -40,7 +40,7 @@ export const getLineId = <T extends HTMLElement>(
40
40
export const getLineNo = < T extends HTMLElement > (
41
41
value ?: number | string | T ,
42
42
) : number | undefined => {
43
- if ( isNone ( value ) ) return undefined ;
43
+ if ( isUndefined ( value ) ) return undefined ;
44
44
45
45
// 行番号のとき
46
46
if ( isNumber ( value ) ) return value ;
@@ -52,7 +52,7 @@ export const getLineNo = <T extends HTMLElement>(
52
52
export const getLine = < T extends HTMLElement > (
53
53
value ?: number | string | T ,
54
54
) : Line | undefined => {
55
- if ( isNone ( value ) ) return undefined ;
55
+ if ( isUndefined ( value ) ) return undefined ;
56
56
57
57
// 行番号のとき
58
58
if ( isNumber ( value ) ) return getLines ( ) [ value ] ;
@@ -64,7 +64,7 @@ export const getLine = <T extends HTMLElement>(
64
64
export const getBaseLine = < T extends HTMLElement > (
65
65
value ?: number | string | T ,
66
66
) : BaseLine | undefined => {
67
- if ( isNone ( value ) ) return undefined ;
67
+ if ( isUndefined ( value ) ) return undefined ;
68
68
69
69
// 行番号のとき
70
70
if ( isNumber ( value ) ) return takeInternalLines ( ) [ value ] ;
@@ -79,9 +79,9 @@ export const getLineDOM = <T extends HTMLElement>(
79
79
if ( isLineDOM ( value ) ) return value ;
80
80
81
81
const id = getLineId ( value ) ;
82
- if ( isNone ( id ) ) return id ;
82
+ if ( isUndefined ( id ) ) return id ;
83
83
const line = document . getElementById ( `L${ id } ` ) ;
84
- if ( isNone ( line ) ) return undefined ;
84
+ if ( isUndefined ( line ) ) return undefined ;
85
85
return line as HTMLDivElement ;
86
86
} ;
87
87
export const isLineDOM = ( dom : unknown ) : dom is HTMLDivElement =>
@@ -90,15 +90,14 @@ export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
90
90
export const getLineCount = ( ) : number => takeInternalLines ( ) . length ;
91
91
92
92
export const getLines = ( ) : readonly Line [ ] => {
93
- const lines = getCachedLines ( ) ;
94
- ensureArray < Line > ( lines , "scrapbox.Page.lines" ) ;
95
- return lines ;
93
+ const lines = ensure ( getCachedLines ( ) , isArray ) ;
94
+ return lines as Line [ ] ;
96
95
} ;
97
96
98
97
export const getText = < T extends HTMLElement > (
99
98
value ?: number | string | T ,
100
99
) : string | undefined => {
101
- if ( isNone ( value ) ) return undefined ;
100
+ if ( isUndefined ( value ) ) return undefined ;
102
101
103
102
// 数字と文字列は行として扱う
104
103
if ( isNumber ( value ) || isString ( value ) ) return getBaseLine ( value ) ?. text ;
@@ -118,7 +117,7 @@ export const getText = <T extends HTMLElement>(
118
117
//中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す
119
118
const chars = [ ] as number [ ] ;
120
119
const line = getBaseLine ( value ) ;
121
- if ( isNone ( line ) ) return ;
120
+ if ( isUndefined ( line ) ) return ;
122
121
for ( const dom of getChars ( value ) ) {
123
122
chars . push ( getIndex ( dom ) ) ;
124
123
}
@@ -127,30 +126,30 @@ export const getText = <T extends HTMLElement>(
127
126
128
127
export const getExternalLink = ( dom : HTMLElement ) : HTMLElement | undefined => {
129
128
const link = dom . closest ( ".link" ) ;
130
- if ( isNone ( link ) ) return undefined ;
129
+ if ( isUndefined ( link ) ) return undefined ;
131
130
return link as HTMLElement ;
132
131
} ;
133
132
export const getInternalLink = ( dom : HTMLElement ) : HTMLElement | undefined => {
134
133
const link = dom . closest ( ".page-link" ) ;
135
- if ( isNone ( link ) ) return undefined ;
134
+ if ( isUndefined ( link ) ) return undefined ;
136
135
return link as HTMLElement ;
137
136
} ;
138
137
export const getLink = ( dom : HTMLElement ) : HTMLElement | undefined => {
139
138
const link = dom . closest ( ".link, .page-link" ) ;
140
- if ( isNone ( link ) ) return undefined ;
139
+ if ( isUndefined ( link ) ) return undefined ;
141
140
return link as HTMLElement ;
142
141
} ;
143
142
144
143
export const getFormula = ( dom : HTMLElement ) : HTMLElement | undefined => {
145
144
const formula = dom . closest ( ".formula" ) ;
146
- if ( isNone ( formula ) ) return undefined ;
145
+ if ( isUndefined ( formula ) ) return undefined ;
147
146
return formula as HTMLElement ;
148
147
} ;
149
148
export const getNextLine = < T extends HTMLElement > (
150
149
value ?: number | string | T ,
151
150
) : Line | undefined => {
152
151
const index = getLineNo ( value ) ;
153
- if ( isNone ( index ) ) return undefined ;
152
+ if ( isUndefined ( index ) ) return undefined ;
154
153
155
154
return getLine ( index + 1 ) ;
156
155
} ;
@@ -159,26 +158,26 @@ export const getPrevLine = <T extends HTMLElement>(
159
158
value ?: number | string | T ,
160
159
) : Line | undefined => {
161
160
const index = getLineNo ( value ) ;
162
- if ( isNone ( index ) ) return undefined ;
161
+ if ( isUndefined ( index ) ) return undefined ;
163
162
164
163
return getLine ( index - 1 ) ;
165
164
} ;
166
165
167
166
export const getHeadLineDOM = ( ) : HTMLDivElement | undefined => {
168
167
const line = lines ( ) ?. firstElementChild ;
169
- if ( isNone ( line ) ) return undefined ;
168
+ if ( isUndefined ( line ) ) return undefined ;
170
169
return line as HTMLDivElement ;
171
170
} ;
172
171
export const getTailLineDOM = ( ) : HTMLDivElement | undefined => {
173
172
const line = lines ( ) ?. lastElementChild ;
174
- if ( isNone ( line ) ) return undefined ;
173
+ if ( isUndefined ( line ) ) return undefined ;
175
174
return line as HTMLDivElement ;
176
175
} ;
177
176
export const getIndentCount = < T extends HTMLElement > (
178
177
value ?: number | string | T ,
179
178
) : number | undefined => {
180
179
const text = getText ( value ) ;
181
- if ( isNone ( text ) ) return undefined ;
180
+ if ( isUndefined ( text ) ) return undefined ;
182
181
return Text . getIndentCount ( text ) ;
183
182
} ;
184
183
/** 指定した行の配下にある行の数を返す
@@ -189,7 +188,7 @@ export const getIndentLineCount = <T extends HTMLElement>(
189
188
value ?: number | string | T ,
190
189
) : number | undefined => {
191
190
const index = getLineNo ( value ) ;
192
- if ( isNone ( index ) ) return ;
191
+ if ( isUndefined ( index ) ) return ;
193
192
return Text . getIndentLineCount ( index , getLines ( ) ) ;
194
193
} ;
195
194
@@ -210,7 +209,7 @@ export const getIndex = (dom: HTMLSpanElement): number => {
210
209
if ( ! isCharDOM ( dom ) ) throw Error ( "A char DOM is required." ) ;
211
210
212
211
const index = dom . className . match ( / c - ( \d + ) / ) ?. [ 1 ] ;
213
- if ( isNone ( index ) ) throw Error ( '.char-index must have ".c-{\\d}"' ) ;
212
+ if ( isUndefined ( index ) ) throw Error ( '.char-index must have ".c-{\\d}"' ) ;
214
213
return parseInt ( index ) ;
215
214
} ;
216
215
export const getHeadCharDOM = (
@@ -242,7 +241,7 @@ export const getDOMFromPoint = (
242
241
const char = targets . find ( ( target ) => isCharDOM ( target ) ) ;
243
242
const line = targets . find ( ( target ) => isLineDOM ( target ) ) ;
244
243
return {
245
- char : isNone ( char ) ? undefined : char as HTMLSpanElement ,
246
- line : isNone ( line ) ? undefined : line as HTMLDivElement ,
244
+ char : isUndefined ( char ) ? undefined : char as HTMLSpanElement ,
245
+ line : isUndefined ( line ) ? undefined : line as HTMLDivElement ,
247
246
} ;
248
247
} ;
0 commit comments