@pfmcodes/caret 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -22
- package/commonjs/editor.js +272 -0
- package/commonjs/index.js +10 -0
- package/commonjs/languages.js +97 -0
- package/commonjs/theme.js +18 -0
- package/esm/editor.js +69 -19
- package/esm/languages.js +54 -3
- package/package.json +2 -2
- package/types/editor.d.ts +156 -16
- package/types/language-utils.d.ts +428 -0
- package/types/languages.d.ts +128 -13
- package/types/types.d.ts +347 -0
package/types/languages.d.ts
CHANGED
|
@@ -1,28 +1,143 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Caret Editor - Languages Module Type Definitions
|
|
3
|
+
* Handles language registration and management for syntax highlighting
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { HLJSApi, LanguageFn } from 'highlight.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Supported languages that are pre-registered by default
|
|
10
|
+
*/
|
|
11
|
+
export type PreRegisteredLanguage =
|
|
12
|
+
| 'javascript'
|
|
13
|
+
| 'js'
|
|
14
|
+
| 'xml'
|
|
15
|
+
| 'html'
|
|
16
|
+
| 'svg'
|
|
17
|
+
| 'css'
|
|
18
|
+
| 'python'
|
|
19
|
+
| 'java'
|
|
20
|
+
| 'csharp'
|
|
21
|
+
| 'cpp'
|
|
22
|
+
| 'ruby'
|
|
23
|
+
| 'php'
|
|
24
|
+
| 'go'
|
|
25
|
+
| 'c'
|
|
26
|
+
| 'rust'
|
|
27
|
+
| 'kotlin'
|
|
28
|
+
| 'swift'
|
|
29
|
+
| 'typescript'
|
|
30
|
+
| 'json'
|
|
31
|
+
| 'bash'
|
|
32
|
+
| 'shell'
|
|
33
|
+
| 'sh'
|
|
34
|
+
| 'plaintext';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Language registration status
|
|
38
|
+
*/
|
|
39
|
+
export interface LanguageRegistration {
|
|
40
|
+
/**
|
|
41
|
+
* Language identifier/name
|
|
42
|
+
*/
|
|
43
|
+
name: string;
|
|
2
44
|
|
|
3
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Whether the language is currently registered
|
|
47
|
+
*/
|
|
48
|
+
isRegistered: boolean;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Aliases for this language
|
|
52
|
+
*/
|
|
53
|
+
aliases?: string[];
|
|
54
|
+
}
|
|
4
55
|
|
|
5
56
|
/**
|
|
6
|
-
*
|
|
57
|
+
* Languages module interface
|
|
7
58
|
*/
|
|
8
|
-
|
|
59
|
+
export interface LanguagesModule {
|
|
9
60
|
/**
|
|
10
|
-
*
|
|
61
|
+
* Initialize and register all default languages
|
|
62
|
+
* This should be called once before using the editor
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import languages from './languages.js';
|
|
67
|
+
* languages.init();
|
|
68
|
+
* ```
|
|
11
69
|
*/
|
|
12
70
|
init(): void;
|
|
13
71
|
|
|
14
72
|
/**
|
|
15
|
-
*
|
|
73
|
+
* Array of currently registered language identifiers
|
|
74
|
+
* This includes both the primary language names and their aliases
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* console.log(languages.registeredLanguages);
|
|
79
|
+
* // ['javascript', 'js', 'xml', 'html', 'python', ...]
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
registeredLanguages: string[];
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Register a new language for syntax highlighting
|
|
86
|
+
* If the language is already registered, this will update its definition
|
|
87
|
+
*
|
|
88
|
+
* @param name - Language identifier (e.g., "rust", "go", "python")
|
|
89
|
+
* @param definition - Highlight.js language definition function
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import languages from './languages.js';
|
|
94
|
+
* import scala from 'highlight.js/es/languages/scala';
|
|
95
|
+
*
|
|
96
|
+
* languages.registerLanguage('scala', scala);
|
|
97
|
+
* ```
|
|
16
98
|
*/
|
|
17
|
-
registerLanguage(
|
|
18
|
-
name: string,
|
|
19
|
-
language: (hljs: typeof import('./highlight.js/es/core.js')) => any
|
|
20
|
-
): void;
|
|
99
|
+
registerLanguage(name: string, definition: LanguageFn): void;
|
|
21
100
|
|
|
22
101
|
/**
|
|
23
|
-
*
|
|
102
|
+
* Reference to the Highlight.js core instance
|
|
103
|
+
* Provides direct access to all Highlight.js methods
|
|
24
104
|
*/
|
|
25
|
-
hljs:
|
|
26
|
-
}
|
|
105
|
+
hljs: HLJSApi;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Default export - Languages module instance
|
|
110
|
+
*/
|
|
111
|
+
declare const languages: LanguagesModule;
|
|
27
112
|
|
|
28
113
|
export default languages;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Helper type to check if a language is registered
|
|
117
|
+
*/
|
|
118
|
+
export type IsLanguageRegistered<T extends string> = T extends PreRegisteredLanguage
|
|
119
|
+
? true
|
|
120
|
+
: boolean;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Language definition map for custom language registration
|
|
124
|
+
*/
|
|
125
|
+
export interface LanguageDefinitionMap {
|
|
126
|
+
[languageName: string]: LanguageFn;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Configuration for bulk language registration
|
|
131
|
+
*/
|
|
132
|
+
export interface BulkLanguageConfig {
|
|
133
|
+
/**
|
|
134
|
+
* Map of language names to their definition functions
|
|
135
|
+
*/
|
|
136
|
+
languages: LanguageDefinitionMap;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether to skip already registered languages
|
|
140
|
+
* @default true
|
|
141
|
+
*/
|
|
142
|
+
skipIfRegistered?: boolean;
|
|
143
|
+
}
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caret Editor - Comprehensive Type Definitions
|
|
3
|
+
* Extended types for internal structures and utilities
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Internal DOM element IDs used by the Caret editor
|
|
8
|
+
*/
|
|
9
|
+
export const enum CaretElementIds {
|
|
10
|
+
TEXTAREA = "Caret-textarea",
|
|
11
|
+
HIGHLIGHTED = "Caret-highlighted",
|
|
12
|
+
CARET = "Caret-caret",
|
|
13
|
+
LINE_COUNTER = "Caret-lineCounter",
|
|
14
|
+
THEME = "Caret-theme"
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* CSS class names used by the Caret editor
|
|
19
|
+
*/
|
|
20
|
+
export const enum CaretClassNames {
|
|
21
|
+
LINE_NUMBER = "Caret-lineCounter-number"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default editor dimensions
|
|
26
|
+
*/
|
|
27
|
+
export interface EditorDimensions {
|
|
28
|
+
width: string;
|
|
29
|
+
height: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Editor style configuration
|
|
34
|
+
*/
|
|
35
|
+
export interface EditorStyle {
|
|
36
|
+
position: string;
|
|
37
|
+
width: string;
|
|
38
|
+
height: string;
|
|
39
|
+
overflow: string;
|
|
40
|
+
fontSize: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Theme configuration
|
|
45
|
+
*/
|
|
46
|
+
export interface ThemeConfig {
|
|
47
|
+
/**
|
|
48
|
+
* Whether the theme is a dark theme
|
|
49
|
+
*/
|
|
50
|
+
isDark: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Color for the caret
|
|
54
|
+
*/
|
|
55
|
+
caretColor: string;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Color for line numbers
|
|
59
|
+
*/
|
|
60
|
+
lineColor: string;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Path to the theme CSS file
|
|
64
|
+
*/
|
|
65
|
+
themePath: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Caret position information
|
|
70
|
+
*/
|
|
71
|
+
export interface CaretPosition {
|
|
72
|
+
/**
|
|
73
|
+
* Horizontal position in pixels
|
|
74
|
+
*/
|
|
75
|
+
left: number;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Vertical position in pixels
|
|
79
|
+
*/
|
|
80
|
+
top: number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Height of the caret in pixels
|
|
84
|
+
*/
|
|
85
|
+
height: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Text selection information
|
|
90
|
+
*/
|
|
91
|
+
export interface TextSelection {
|
|
92
|
+
/**
|
|
93
|
+
* Start position of the selection
|
|
94
|
+
*/
|
|
95
|
+
start: number;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* End position of the selection
|
|
99
|
+
*/
|
|
100
|
+
end: number;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Selected text content
|
|
104
|
+
*/
|
|
105
|
+
text: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Line information for the editor
|
|
110
|
+
*/
|
|
111
|
+
export interface LineInfo {
|
|
112
|
+
/**
|
|
113
|
+
* Line number (1-indexed)
|
|
114
|
+
*/
|
|
115
|
+
lineNumber: number;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Line content
|
|
119
|
+
*/
|
|
120
|
+
content: string;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Starting character index in the full text
|
|
124
|
+
*/
|
|
125
|
+
startIndex: number;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Ending character index in the full text
|
|
129
|
+
*/
|
|
130
|
+
endIndex: number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Indentation configuration
|
|
135
|
+
*/
|
|
136
|
+
export interface IndentConfig {
|
|
137
|
+
/**
|
|
138
|
+
* Indentation string (spaces or tab)
|
|
139
|
+
* @default " " (4 spaces)
|
|
140
|
+
*/
|
|
141
|
+
indent: string;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether to use tabs instead of spaces
|
|
145
|
+
* @default false
|
|
146
|
+
*/
|
|
147
|
+
useTabs?: boolean;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Number of spaces per indent level
|
|
151
|
+
* @default 4
|
|
152
|
+
*/
|
|
153
|
+
tabSize?: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Event handlers for editor events
|
|
158
|
+
*/
|
|
159
|
+
export interface CaretEditorEventHandlers {
|
|
160
|
+
/**
|
|
161
|
+
* Called when the editor content changes
|
|
162
|
+
*/
|
|
163
|
+
onInput?: (value: string) => void;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Called when the editor gains focus
|
|
167
|
+
*/
|
|
168
|
+
onFocus?: () => void;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Called when the editor loses focus
|
|
172
|
+
*/
|
|
173
|
+
onBlur?: () => void;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Called when the editor is scrolled
|
|
177
|
+
*/
|
|
178
|
+
onScroll?: (scrollLeft: number, scrollTop: number) => void;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Called when a key is pressed
|
|
182
|
+
*/
|
|
183
|
+
onKeyDown?: (event: KeyboardEvent) => void;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Called when a key is released
|
|
187
|
+
*/
|
|
188
|
+
onKeyUp?: (event: KeyboardEvent) => void;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Called when the caret position changes
|
|
192
|
+
*/
|
|
193
|
+
onCaretMove?: (position: CaretPosition) => void;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Called when the text selection changes
|
|
197
|
+
*/
|
|
198
|
+
onSelectionChange?: (selection: TextSelection) => void;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Extended configuration with event handlers and additional options
|
|
203
|
+
*/
|
|
204
|
+
export interface ExtendedCaretEditorConfig extends CaretEditorConfig {
|
|
205
|
+
/**
|
|
206
|
+
* Event handlers
|
|
207
|
+
*/
|
|
208
|
+
events?: CaretEditorEventHandlers;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Custom indentation settings
|
|
212
|
+
*/
|
|
213
|
+
indentation?: IndentConfig;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Custom editor dimensions
|
|
217
|
+
*/
|
|
218
|
+
dimensions?: EditorDimensions;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Whether to enable spell checking
|
|
222
|
+
* @default false
|
|
223
|
+
*/
|
|
224
|
+
spellcheck?: boolean;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Whether to disable autocapitalization
|
|
228
|
+
* @default true
|
|
229
|
+
*/
|
|
230
|
+
disableAutocapitalize?: boolean;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Whether to show line numbers
|
|
234
|
+
* @default true
|
|
235
|
+
*/
|
|
236
|
+
showLineNumbers?: boolean;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Keyboard event information for Tab handling
|
|
241
|
+
*/
|
|
242
|
+
export interface TabKeyEvent {
|
|
243
|
+
/**
|
|
244
|
+
* Whether Shift key was pressed
|
|
245
|
+
*/
|
|
246
|
+
shiftKey: boolean;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Current cursor/selection start
|
|
250
|
+
*/
|
|
251
|
+
selectionStart: number;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Current cursor/selection end
|
|
255
|
+
*/
|
|
256
|
+
selectionEnd: number;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Current editor value
|
|
260
|
+
*/
|
|
261
|
+
value: string;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Result of tab key processing
|
|
266
|
+
*/
|
|
267
|
+
export interface TabKeyResult {
|
|
268
|
+
/**
|
|
269
|
+
* New value after indentation/unindentation
|
|
270
|
+
*/
|
|
271
|
+
newValue: string;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* New selection start position
|
|
275
|
+
*/
|
|
276
|
+
newSelectionStart: number;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* New selection end position
|
|
280
|
+
*/
|
|
281
|
+
newSelectionEnd: number;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Utility type for Highlight.js language names
|
|
286
|
+
* Common languages - extend as needed
|
|
287
|
+
*/
|
|
288
|
+
export type HighlightLanguage =
|
|
289
|
+
| "javascript"
|
|
290
|
+
| "typescript"
|
|
291
|
+
| "python"
|
|
292
|
+
| "java"
|
|
293
|
+
| "csharp"
|
|
294
|
+
| "cpp"
|
|
295
|
+
| "c"
|
|
296
|
+
| "go"
|
|
297
|
+
| "rust"
|
|
298
|
+
| "php"
|
|
299
|
+
| "ruby"
|
|
300
|
+
| "swift"
|
|
301
|
+
| "kotlin"
|
|
302
|
+
| "scala"
|
|
303
|
+
| "html"
|
|
304
|
+
| "css"
|
|
305
|
+
| "scss"
|
|
306
|
+
| "less"
|
|
307
|
+
| "json"
|
|
308
|
+
| "xml"
|
|
309
|
+
| "yaml"
|
|
310
|
+
| "markdown"
|
|
311
|
+
| "sql"
|
|
312
|
+
| "bash"
|
|
313
|
+
| "shell"
|
|
314
|
+
| "powershell"
|
|
315
|
+
| "dockerfile"
|
|
316
|
+
| string; // Allow any string for custom languages
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Utility type for Highlight.js theme names
|
|
320
|
+
* Common themes - extend as needed
|
|
321
|
+
*/
|
|
322
|
+
export type HighlightTheme =
|
|
323
|
+
| "hybrid"
|
|
324
|
+
| "monokai"
|
|
325
|
+
| "github"
|
|
326
|
+
| "atom-one-dark"
|
|
327
|
+
| "atom-one-light"
|
|
328
|
+
| "vs"
|
|
329
|
+
| "vs2015"
|
|
330
|
+
| "dracula"
|
|
331
|
+
| "nord"
|
|
332
|
+
| "solarized-dark"
|
|
333
|
+
| "solarized-light"
|
|
334
|
+
| "tomorrow"
|
|
335
|
+
| "tomorrow-night"
|
|
336
|
+
| string; // Allow any string for custom themes
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Re-export main types for convenience
|
|
340
|
+
*/
|
|
341
|
+
export type {
|
|
342
|
+
CaretEditorConfig,
|
|
343
|
+
CaretEditorInstance,
|
|
344
|
+
CaretEditor,
|
|
345
|
+
FontMetrics,
|
|
346
|
+
LanguageManager
|
|
347
|
+
} from "./index";
|