draftly 0.1.0-alpha.0

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.
@@ -0,0 +1,257 @@
1
+ import * as _codemirror_state from '@codemirror/state';
2
+ import { Range, Extension, Facet } from '@codemirror/state';
3
+ import { EditorView, Decoration, KeyBinding, ViewUpdate, WidgetType, ViewPlugin, DecorationSet } from '@codemirror/view';
4
+ import { MarkdownConfig } from '@lezer/markdown';
5
+
6
+ /**
7
+ * Context passed to plugin lifecycle methods
8
+ */
9
+ interface PluginContext {
10
+ /** Current configuration */
11
+ readonly config: DraftlyConfig;
12
+ }
13
+ /**
14
+ * Plugin configuration schema
15
+ */
16
+ interface PluginConfig {
17
+ [key: string]: unknown;
18
+ }
19
+ /**
20
+ * Decoration context passed to plugin decoration builders
21
+ * Provides access to view state and decoration collection
22
+ */
23
+ interface DecorationContext {
24
+ /** The EditorView instance (readonly) */
25
+ readonly view: EditorView;
26
+ /** Array to push decorations into (will be sorted automatically) */
27
+ readonly decorations: Range<Decoration>[];
28
+ /** Check if selection overlaps with a range (to show raw markdown) */
29
+ selectionOverlapsRange(from: number, to: number): boolean;
30
+ /** Check if cursor is within a range */
31
+ cursorInRange(from: number, to: number): boolean;
32
+ }
33
+ /**
34
+ * Abstract base class for all draftly plugins
35
+ *
36
+ * Implements OOP principles:
37
+ * - Abstraction: abstract name/version must be implemented by subclasses
38
+ * - Encapsulation: private _config, protected _context
39
+ * - Inheritance: specialized plugin classes can extend this
40
+ */
41
+ declare abstract class DraftlyPlugin {
42
+ /** Unique plugin identifier (abstract - must be implemented) */
43
+ abstract readonly name: string;
44
+ /** Plugin version (abstract - must be implemented) */
45
+ abstract readonly version: string;
46
+ /** Plugin dependencies - names of required plugins */
47
+ readonly dependencies: string[];
48
+ /** Private configuration storage */
49
+ private _config;
50
+ /** Protected context - accessible to subclasses */
51
+ protected _context: PluginContext | null;
52
+ /** Get plugin configuration */
53
+ get config(): PluginConfig;
54
+ /** Set plugin configuration */
55
+ set config(value: PluginConfig);
56
+ /** Get plugin context */
57
+ get context(): PluginContext | null;
58
+ /**
59
+ * Return CodeMirror extensions for this plugin
60
+ * Override to provide custom extensions
61
+ */
62
+ getExtensions(): Extension[];
63
+ /**
64
+ * Return markdown parser extensions
65
+ * Override to extend markdown parsing
66
+ */
67
+ getMarkdownConfig(): MarkdownConfig | null;
68
+ /**
69
+ * Return keybindings for this plugin
70
+ * Override to add custom keyboard shortcuts
71
+ */
72
+ getKeymap(): KeyBinding[];
73
+ /**
74
+ * Decoration priority (higher = applied later)
75
+ * Override to customize priority. Default: 100
76
+ */
77
+ get decorationPriority(): number;
78
+ /**
79
+ * Build decorations for the current view state
80
+ * Override to contribute decorations to the editor
81
+ *
82
+ * @param ctx - Decoration context with view and decoration array
83
+ */
84
+ buildDecorations(_ctx: DecorationContext): void;
85
+ /**
86
+ * Called when plugin is registered with draftly
87
+ * Override to perform initialization
88
+ *
89
+ * @param context - Plugin context with configuration
90
+ */
91
+ onRegister(context: PluginContext): void | Promise<void>;
92
+ /**
93
+ * Called when plugin is unregistered
94
+ * Override to perform cleanup
95
+ */
96
+ onUnregister(): void;
97
+ /**
98
+ * Called when EditorView is created and ready
99
+ * Override to perform view-specific initialization
100
+ *
101
+ * @param view - The EditorView instance
102
+ */
103
+ onViewReady(_view: EditorView): void;
104
+ /**
105
+ * Called on view updates (document changes, selection changes, etc.)
106
+ * Override to react to editor changes
107
+ *
108
+ * @param update - The ViewUpdate with change information
109
+ */
110
+ onViewUpdate(_update: ViewUpdate): void;
111
+ /**
112
+ * Return ARIA attributes to add to the editor
113
+ * Override to improve accessibility
114
+ */
115
+ getAriaAttributes(): Record<string, string>;
116
+ /**
117
+ * Return widget types this plugin provides
118
+ * Override to contribute custom widgets
119
+ */
120
+ getWidgets(): WidgetType[];
121
+ /**
122
+ * Helper to get current editor state
123
+ * @param view - The EditorView instance
124
+ */
125
+ protected getState(view: EditorView): _codemirror_state.EditorState;
126
+ /**
127
+ * Helper to get current document
128
+ * @param view - The EditorView instance
129
+ */
130
+ protected getDocument(view: EditorView): _codemirror_state.Text;
131
+ }
132
+ /**
133
+ * Base class for plugins that primarily contribute decorations
134
+ * Extends DraftlyPlugin with decoration-focused defaults
135
+ */
136
+ declare abstract class DecorationPlugin extends DraftlyPlugin {
137
+ /**
138
+ * Decoration priority - lower than default for decoration plugins
139
+ * Override to customize
140
+ */
141
+ get decorationPriority(): number;
142
+ /**
143
+ * Subclasses must implement this to provide decorations
144
+ * @param ctx - Decoration context
145
+ */
146
+ abstract buildDecorations(ctx: DecorationContext): void;
147
+ }
148
+ /**
149
+ * Base class for plugins that add syntax/parser extensions
150
+ * Extends DraftlyPlugin with syntax-focused requirements
151
+ */
152
+ declare abstract class SyntaxPlugin extends DraftlyPlugin {
153
+ /**
154
+ * Subclasses must implement this to provide markdown config
155
+ */
156
+ abstract getMarkdownConfig(): MarkdownConfig;
157
+ }
158
+
159
+ /**
160
+ * DraftlyNode: represents a node in the markdown tree
161
+ *
162
+ * Useful for debugging and development
163
+ */
164
+ type DraftlyNode = {
165
+ from: number;
166
+ to: number;
167
+ name: string;
168
+ children: DraftlyNode[];
169
+ isSelected: boolean;
170
+ };
171
+ /**
172
+ * Configuration options for the draftly editor
173
+ */
174
+ interface DraftlyConfig {
175
+ /** Plugins to load */
176
+ plugins?: DraftlyPlugin[];
177
+ /** Additional markdown extensions for the parser */
178
+ markdown?: MarkdownConfig[];
179
+ /** Additional CodeMirror extensions */
180
+ extensions?: Extension[];
181
+ /** Additional keybindings */
182
+ keymap?: KeyBinding[];
183
+ /** Disable the built-in view plugin (for raw markdown mode) */
184
+ disableViewPlugin?: boolean;
185
+ /** Enable default keybindings */
186
+ defaultKeybindings?: boolean;
187
+ /** Enable history */
188
+ history?: boolean;
189
+ /** Enable indent with tab */
190
+ indentWithTab?: boolean;
191
+ /** Draw selection */
192
+ drawSelection?: boolean;
193
+ /** Highlight active line */
194
+ highlightActiveLine?: boolean;
195
+ /** Rectangular selection */
196
+ rectangularSelection?: boolean;
197
+ /** Line wrapping in raw markdown mode */
198
+ lineWrapping?: boolean;
199
+ /** Callback to receive the nodes on every update */
200
+ onNodesChange?: (nodes: DraftlyNode[]) => void;
201
+ }
202
+ /**
203
+ * Creates a draftly editor extension bundle for CodeMirror 6
204
+ *
205
+ * @param config - Configuration options for the editor
206
+ * @returns CodeMirror Extension that can be added to EditorState
207
+ *
208
+ * @example
209
+ * ```ts
210
+ * import { EditorView } from '@codemirror/view';
211
+ * import { EditorState } from '@codemirror/state';
212
+ * import { draftly } from 'draftly';
213
+ *
214
+ * const view = new EditorView({
215
+ * state: EditorState.create({
216
+ * doc: '# Hello draftly',
217
+ * extensions: [draftly()]
218
+ * }),
219
+ * parent: document.getElementById('editor')
220
+ * });
221
+ * ```
222
+ */
223
+ declare function draftly(config?: DraftlyConfig): Extension[];
224
+
225
+ /**
226
+ * Facet to register plugins with the view plugin
227
+ */
228
+ declare const DraftlyPluginsFacet: Facet<DraftlyPlugin[], DraftlyPlugin[]>;
229
+ /**
230
+ * Facet to register the onNodesChange callback
231
+ */
232
+ declare const draftlyOnNodesChangeFacet: Facet<((nodes: DraftlyNode[]) => void) | undefined, ((nodes: DraftlyNode[]) => void) | undefined>;
233
+ /**
234
+ * draftly View Plugin
235
+ * Handles rich markdown rendering with decorations
236
+ */
237
+ declare class draftlyViewPluginClass {
238
+ decorations: DecorationSet;
239
+ private plugins;
240
+ private onNodesChange?;
241
+ constructor(view: EditorView);
242
+ update(update: ViewUpdate): void;
243
+ private buildNodes;
244
+ }
245
+ /**
246
+ * The main draftly ViewPlugin extension
247
+ */
248
+ declare const draftlyViewPlugin: ViewPlugin<draftlyViewPluginClass, undefined>;
249
+ /**
250
+ * Create draftly view extension bundle with plugin support
251
+ * @param plugins - Optional array of DraftlyPlugin instances
252
+ * @param onNodesChange - Optional callback to receive nodes on every update
253
+ * @returns Extension array including view plugin, theme, and plugin facet
254
+ */
255
+ declare function createDraftlyViewExtension(plugins?: DraftlyPlugin[], onNodesChange?: (nodes: DraftlyNode[]) => void): Extension[];
256
+
257
+ export { type DecorationContext, DecorationPlugin, type DraftlyConfig, type DraftlyNode, DraftlyPlugin, DraftlyPluginsFacet, type PluginConfig, type PluginContext, SyntaxPlugin, createDraftlyViewExtension, draftly as default, draftly, draftlyOnNodesChangeFacet, draftlyViewPlugin };