-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to codemirror 6 #3617
Merged
Merged
Upgrade to codemirror 6 #3617
Conversation
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
Does increase bundle size massively though, Will need to think about solutions for this.
Cannot find existing option for twig/smarty, need to look other methods.
Updated content sync and preview scoll sync to work. Many features commented out until they can be updated.
- Updated clipboard handling - Removed old clipboard package for browser-native API. - Updated codemirror editor events to use new props for new data types.
Split out legacy modes to their own dynamically imported bundle to reduce main code bundle size.
Uses our custom event system, uses methods that take callables so that internal dependancies can be passed.
Custom Theme Registration ExampleDetails
<script>
// Theme data taken from:
// https://github.com/craftzdog/cm6-themes/blob/main/packages/solarized-dark/src/index.ts
// (MIT License) - Copyright (C) 2022 by Takuya Matsuyama and others
const base00 = '#002b36',
base01 = '#073642',
base02 = '#586e75',
base03 = '#657b83',
base04 = '#839496',
base05 = '#93a1a1',
base06 = '#eee8d5',
base07 = '#fdf6e3',
base_red = '#dc322f',
base_orange = '#cb4b16',
base_yellow = '#b58900',
base_green = '#859900',
base_cyan = '#2aa198',
base_blue = '#268bd2',
base_violet = '#6c71c4',
base_magenta = '#d33682'
const invalid = '#d30102',
stone = base04,
darkBackground = '#00252f',
highlightBackground = '#173541',
background = base00,
tooltipBackground = base01,
selection = '#173541',
cursor = base04
function viewThemeBuilder() {
return {
'&': {
color: base05,
backgroundColor: background
},
'.cm-content': {
caretColor: cursor
},
'.cm-cursor, .cm-dropCursor': { borderLeftColor: cursor },
'&.cm-focused .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection':
{ backgroundColor: selection },
'.cm-panels': { backgroundColor: darkBackground, color: base03 },
'.cm-panels.cm-panels-top': { borderBottom: '2px solid black' },
'.cm-panels.cm-panels-bottom': { borderTop: '2px solid black' },
'.cm-searchMatch': {
backgroundColor: '#72a1ff59',
outline: '1px solid #457dff'
},
'.cm-searchMatch.cm-searchMatch-selected': {
backgroundColor: '#6199ff2f'
},
'.cm-activeLine': { backgroundColor: highlightBackground },
'.cm-selectionMatch': { backgroundColor: '#aafe661a' },
'&.cm-focused .cm-matchingBracket, &.cm-focused .cm-nonmatchingBracket': {
outline: `1px solid ${base06}`
},
'.cm-gutters': {
backgroundColor: darkBackground,
color: stone,
border: 'none'
},
'.cm-activeLineGutter': {
backgroundColor: highlightBackground
},
'.cm-foldPlaceholder': {
backgroundColor: 'transparent',
border: 'none',
color: '#ddd'
},
'.cm-tooltip': {
border: 'none',
backgroundColor: tooltipBackground
},
'.cm-tooltip .cm-tooltip-arrow:before': {
borderTopColor: 'transparent',
borderBottomColor: 'transparent'
},
'.cm-tooltip .cm-tooltip-arrow:after': {
borderTopColor: tooltipBackground,
borderBottomColor: tooltipBackground
},
'.cm-tooltip-autocomplete': {
'& > ul > li[aria-selected]': {
backgroundColor: highlightBackground,
color: base03
}
}
};
}
function highlightStyleBuilder(t) {
return [
{ tag: t.keyword, color: base_green },
{
tag: [t.name, t.deleted, t.character, t.propertyName, t.macroName],
color: base_cyan
},
{ tag: [t.variableName], color: base05 },
{ tag: [t.function(t.variableName)], color: base_blue },
{ tag: [t.labelName], color: base_magenta },
{
tag: [t.color, t.constant(t.name), t.standard(t.name)],
color: base_yellow
},
{ tag: [t.definition(t.name), t.separator], color: base_cyan },
{ tag: [t.brace], color: base_magenta },
{
tag: [t.annotation],
color: invalid
},
{
tag: [t.number, t.changed, t.annotation, t.modifier, t.self, t.namespace],
color: base_magenta
},
{
tag: [t.typeName, t.className],
color: base_orange
},
{
tag: [t.operator, t.operatorKeyword],
color: base_violet
},
{
tag: [t.tagName],
color: base_blue
},
{
tag: [t.squareBracket],
color: base_red
},
{
tag: [t.angleBracket],
color: base02
},
{
tag: [t.attributeName],
color: base05
},
{
tag: [t.regexp],
color: invalid
},
{
tag: [t.quote],
color: base_green
},
{ tag: [t.string], color: base_yellow },
{
tag: t.link,
color: base_cyan,
textDecoration: 'underline',
textUnderlinePosition: 'under'
},
{
tag: [t.url, t.escape, t.special(t.string)],
color: base_yellow
},
{ tag: [t.meta], color: base_red },
{ tag: [t.comment], color: base02, fontStyle: 'italic' },
{ tag: t.strong, fontWeight: 'bold', color: base06 },
{ tag: t.emphasis, fontStyle: 'italic', color: base_green },
{ tag: t.strikethrough, textDecoration: 'line-through' },
{ tag: t.heading, fontWeight: 'bold', color: base_yellow },
{ tag: t.heading1, fontWeight: 'bold', color: base07 },
{
tag: [t.heading2, t.heading3, t.heading4],
fontWeight: 'bold',
color: base06
},
{
tag: [t.heading5, t.heading6],
color: base06
},
{ tag: [t.atom, t.bool, t.special(t.variableName)], color: base_magenta },
{
tag: [t.processingInstruction, t.inserted, t.contentSeparator],
color: base_red
},
{
tag: [t.contentSeparator],
color: base_yellow
},
{ tag: t.invalid, color: base02, borderBottom: `1px dotted ${base_red}` }
];
}
window.addEventListener('library-cm6::configure-theme', event => {
const detail = event.detail;
detail.registerViewTheme(viewThemeBuilder);
detail.registerHighlightStyle(highlightStyleBuilder);
});
</script> |
Required monkey-patch to work around potential codemirror issue with shadowdom+iframe usage. Also updated JS packages to latest versions.
New simple interface added for abstraction of CM editor in simple use-cases, just to provide common actions like get/set content, focus and set mode.
- Fixed some keybindings not running as expected, due to some editor defaults overriding or further actions taking place since the action would not indicate it's been dealt with (by returning boolean). - Fixed spacing/border-radius being used on codeblocks on non-intended areas like the MD editor. - Fixed lack of BG on default light theme, visible on full screen md editor. - Fixed error thrown when the user does not have access to change the current editor (Likely non-cm related existing issue)
- Updated event naming to be "cm6" when codemirror-specific. - Removed cm block border in md editor to prevent double bordering. - Updated copy handling to fallback to execCommand.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR to track upgrade from Codemirror 5 to 6. Quite a large change.
Closes #3518
References
Progress Report
Got a lot of fundamentals in place but concerned about the growth of complexity here.
Specifically:
The growth of bundle size, might need to do finer splitting.Added extra splitThe complex requirements of a theme. They're really designed to be bundled. Here's an example of an external theme. Don't think we can just define a string and import global JS like before. We could pass all required dependencies through a configure event but the theme implementation will probably be quite custom to BookStack. Or can we provide a simpler abstraction?tags
. Is a custom API so external themes will need adapting.Probable going to progress this slowly across a number of months, to understand codemirror rate of change and to gain some perspectives on best approach.
Todo
window.codeTheme
.cm6
instead ofcm
.Only add copy button if in secure contextAdded execCommand fallback instead.Docs/API Updates
codeMirrorInstance
in the event data. It instead has acmEditorView
property that contains the CodeMirror EditorView instance for the Markdown editor.editor-markdown-cm6::pre-init
and no longer containsconfig
in the event data. It instead has aeditorViewConfig
property that contains the CodeMirror EditorViewConfig data that's used to create the EditorView. Config object can be manipulated before use.window.codeTheme
is no longer used to set themes. Instead, there's alibrary-cm6::configure-theme
event which emits event data in the following format: