-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Improve beta editor support for the Rich Markdown plugin (#9935
- Loading branch information
1 parent
17a8ce5
commit c35085d
Showing
12 changed files
with
152 additions
and
17 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
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
83 changes: 83 additions & 0 deletions
83
packages/editor/CodeMirror/CodeMirror5Emulation/CodeMirror5BuiltInOptions.ts
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 { Compartment, Extension, RangeSetBuilder, StateEffect } from '@codemirror/state'; | ||
import { Decoration, DecorationSet, EditorView, ViewPlugin, ViewUpdate } from '@codemirror/view'; | ||
|
||
const activeLineDecoration = Decoration.line({ class: 'CodeMirror-activeline CodeMirror-activeline-background' }); | ||
|
||
const optionToExtension: Record<string, Extension> = { | ||
'styleActiveLine': [ | ||
ViewPlugin.fromClass(class { | ||
public decorations: DecorationSet; | ||
|
||
public constructor(view: EditorView) { | ||
this.updateDecorations(view); | ||
} | ||
|
||
public update(update: ViewUpdate) { | ||
this.updateDecorations(update.view); | ||
} | ||
|
||
private updateDecorations(view: EditorView) { | ||
const builder = new RangeSetBuilder<Decoration>(); | ||
let lastLine = -1; | ||
|
||
for (const selection of view.state.selection.ranges) { | ||
const startLine = selection.from; | ||
const line = view.state.doc.lineAt(startLine); | ||
|
||
if (line.number !== lastLine) { | ||
builder.add(line.from, line.from, activeLineDecoration); | ||
} | ||
|
||
lastLine = line.number; | ||
} | ||
|
||
this.decorations = builder.finish(); | ||
} | ||
}, { | ||
decorations: plugin => plugin.decorations, | ||
}), | ||
EditorView.baseTheme({ | ||
'&dark .CodeMirror-activeline-background': { | ||
background: '#3304', | ||
color: 'white', | ||
}, | ||
'&light .CodeMirror-activeline-background': { | ||
background: '#7ff4', | ||
color: 'black', | ||
}, | ||
}), | ||
], | ||
}; | ||
|
||
// Maps several CM5 options to CM6 extensions | ||
export default class CodeMirror5BuiltInOptions { | ||
private activeOptions: string[] = []; | ||
private extensionCompartment: Compartment = new Compartment(); | ||
|
||
public constructor(private editor: EditorView) { | ||
editor.dispatch({ | ||
effects: StateEffect.appendConfig.of(this.extensionCompartment.of([])), | ||
}); | ||
} | ||
|
||
private updateExtensions() { | ||
const extensions = this.activeOptions.map(option => optionToExtension[option]); | ||
this.editor.dispatch({ | ||
effects: this.extensionCompartment.reconfigure(extensions), | ||
}); | ||
} | ||
|
||
public supportsOption(option: string) { | ||
return optionToExtension.hasOwnProperty(option); | ||
} | ||
|
||
public setOption(optionName: string, value: boolean) { | ||
this.activeOptions = this.activeOptions.filter(other => other !== optionName); | ||
|
||
if (value) { | ||
this.activeOptions.push(optionName); | ||
} | ||
|
||
this.updateExtensions(); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -91,4 +91,5 @@ activatable | |
titlewrapper | ||
notyf | ||
Notyf | ||
Prec | ||
activeline | ||
Prec |