-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
326 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
} | ||
</style> | ||
<script> | ||
MarkEdit = {}; | ||
window.config = "{{EDITOR_CONFIG}}"; | ||
</script> | ||
</head> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { EditorView } from '@codemirror/view'; | ||
import { EditorSelection, EditorState, Text } from '@codemirror/state'; | ||
import { syntaxTree } from '@codemirror/language'; | ||
import { TextEditable, TextRange } from 'markedit-api'; | ||
import { redo, undo } from '../@vendor/commands/history'; | ||
|
||
/** | ||
* TextEditable implementation to provide convenient text editing interfaces. | ||
*/ | ||
export class TextEditor implements TextEditable { | ||
setView(view: EditorView) { | ||
this.view = view; | ||
} | ||
|
||
getText(range?: TextRange): string { | ||
if (range === undefined) { | ||
return this.doc.toString(); | ||
} | ||
|
||
const { from, to } = range; | ||
return this.doc.sliceString(from, to); | ||
} | ||
|
||
setText(text: string, range?: TextRange): void { | ||
const from = range === undefined ? 0 : range.from; | ||
const to = range === undefined ? this.doc.length : range.to; | ||
this.view.dispatch({ | ||
changes: { from, to, insert: text }, | ||
}); | ||
} | ||
|
||
getSelections(): TextRange[] { | ||
return this.state.selection.ranges.map(({ from, to }) => ({ from, to })); | ||
} | ||
|
||
setSelections(ranges: TextRange[]): void { | ||
const selections = ranges.map(({ from, to }) => EditorSelection.range(from, to)); | ||
this.view.dispatch({ | ||
selection: EditorSelection.create(selections), | ||
}); | ||
} | ||
|
||
getLineNumber(position: number): number { | ||
return this.doc.lineAt(position).number - 1; | ||
} | ||
|
||
getLineRange(row: number): TextRange { | ||
const { from, to } = this.doc.line(row + 1); | ||
return { from, to }; | ||
} | ||
|
||
getLineCount(): number { | ||
return this.doc.lines; | ||
} | ||
|
||
getLineBreak(): string { | ||
return this.state.lineBreak; | ||
} | ||
|
||
getNodeName(position: number): string { | ||
return syntaxTree(this.state).resolve(position).name; | ||
} | ||
|
||
undo(): void { | ||
undo(this.view); | ||
} | ||
|
||
redo(): void { | ||
redo(this.view); | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private view = window.editor; | ||
|
||
private get state(): EditorState { | ||
return this.view.state; | ||
} | ||
|
||
private get doc(): Text { | ||
return this.state.doc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { EditorView } from '@codemirror/view'; | ||
import { Extension } from '@codemirror/state'; | ||
import { MarkdownConfig } from '@lezer/markdown'; | ||
import { markdownExtensionBundle } from '../extensions'; | ||
|
||
export function onEditorReady(listener: (editorView: EditorView) => void) { | ||
storage.editorReadyListeners.push(listener); | ||
|
||
if (isEditorReady()) { | ||
listener(window.editor); | ||
} | ||
} | ||
|
||
export function addExtension(extension: Extension) { | ||
storage.extensions.push(extension); | ||
|
||
if (isEditorReady()) { | ||
window.editor.dispatch({ | ||
effects: window.dynamics.extensionConfigurator?.reconfigure(userExtensions()), | ||
}); | ||
} | ||
} | ||
|
||
export function addMarkdownConfig(config: MarkdownConfig) { | ||
storage.markdownConfigs.push(config); | ||
|
||
if (isEditorReady()) { | ||
window.editor.dispatch({ | ||
effects: window.dynamics.markdownConfigurator?.reconfigure(markdownExtensionBundle()), | ||
}); | ||
} | ||
} | ||
|
||
export function editorReadyListeners() { | ||
return storage.editorReadyListeners; | ||
} | ||
|
||
export function userExtensions(): Extension[] { | ||
return storage.extensions; | ||
} | ||
|
||
export function userMarkdownConfigs(): MarkdownConfig[] { | ||
return storage.markdownConfigs; | ||
} | ||
|
||
function isEditorReady() { | ||
return typeof window.editor.dispatch === 'function'; | ||
} | ||
|
||
const storage: { | ||
editorReadyListeners: ((editorView: EditorView) => void)[]; | ||
extensions: Extension[]; | ||
markdownConfigs: MarkdownConfig[]; | ||
} = { | ||
editorReadyListeners: [], | ||
extensions: [], | ||
markdownConfigs: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as cmView from '@codemirror/view'; | ||
import * as cmState from '@codemirror/state'; | ||
import * as cmLanguage from '@codemirror/language'; | ||
import * as cmCommands from '@codemirror/commands'; | ||
import * as cmSearch from '@codemirror/search'; | ||
|
||
import * as lezerCommon from '@lezer/common'; | ||
import * as lezerHighlight from '@lezer/highlight'; | ||
import * as lezerMarkdown from '@lezer/markdown'; | ||
import * as lezerLr from '@lezer/lr'; | ||
|
||
import * as customHistory from '../@vendor/commands/history'; | ||
|
||
import { TextEditor } from './editor'; | ||
import { onEditorReady, addExtension, addMarkdownConfig } from './methods'; | ||
|
||
export function initMarkEditModules() { | ||
const codemirror = { | ||
view: cmView, | ||
state: cmState, | ||
language: cmLanguage, | ||
commands: { | ||
...cmCommands, | ||
...customHistory, | ||
}, | ||
search: cmSearch, | ||
}; | ||
|
||
const lezer = { | ||
common: lezerCommon, | ||
highlight: lezerHighlight, | ||
markdown: lezerMarkdown, | ||
lr: lezerLr, | ||
}; | ||
|
||
MarkEdit.editorAPI = new TextEditor(); | ||
MarkEdit.codemirror = codemirror; | ||
MarkEdit.lezer = lezer; | ||
|
||
MarkEdit.onEditorReady = onEditorReady; | ||
MarkEdit.addExtension = addExtension; | ||
MarkEdit.addMarkdownConfig = addMarkdownConfig; | ||
|
||
const modules = { | ||
'markedit-api': { MarkEdit }, | ||
'@codemirror/view': codemirror.view, | ||
'@codemirror/state': codemirror.state, | ||
'@codemirror/language': codemirror.language, | ||
'@codemirror/commands': codemirror.commands, | ||
'@codemirror/search': codemirror.search, | ||
'@lezer/common': lezer.common, | ||
'@lezer/highlight': lezer.highlight, | ||
'@lezer/markdown': lezer.markdown, | ||
'@lezer/lr': lezer.lr, | ||
}; | ||
|
||
const require = (id: string) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const module = (modules as any)[id]; | ||
if (module !== undefined) { | ||
return module; | ||
} | ||
|
||
console.error(`Failed to require module: "${id}", supported modules: ${Object.keys(modules).join(', ')}`); | ||
return {}; | ||
}; | ||
|
||
window.require = require as NodeRequire; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.