-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support the beta markdown editor #4
Merged
alondmnt
merged 4 commits into
alondmnt:main
from
personalizedrefrigerator:pr/support-cm6
Mar 23, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| { | ||
| "extraScripts": [] | ||
| "extraScripts": ["contentScript/codeMirror5.ts", "contentScript/codeMirror6.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,49 @@ | ||
| import getSettings from "./getSettings"; | ||
| import type { IndentSettings } from "../settings"; | ||
| import type { PluginContext } from "./types"; | ||
|
|
||
| export default function(context: PluginContext) { | ||
| return { | ||
| plugin: function plugin(CodeMirror: any) { | ||
| if (CodeMirror.cm6) return; | ||
|
|
||
| CodeMirror.defineOption('enable-space-indenter', false, async function(cm: any, val: boolean, _old: boolean) { | ||
| if (val) { | ||
| const settings = await getSettings(context); | ||
| cm.updateIndentSettings(settings); | ||
| } | ||
| }); | ||
|
|
||
| CodeMirror.defineExtension('updateIndentSettings', function(newSettings: IndentSettings) { | ||
| if (!newSettings.indentWithTabs) { | ||
| // based on: https://github.com/codemirror/codemirror5/issues/988#issuecomment-549644684 | ||
| this.setOption('extraKeys', { | ||
| Tab: (cm: any) => { | ||
| const isListItem = /^-|^\*|^\d\./g; | ||
| if (cm.getMode().name === 'null') { | ||
| cm.execCommand('insertTab'); | ||
| } else { | ||
| const line = cm.getLine(cm.getCursor().line).trim(); | ||
| if (cm.somethingSelected() || | ||
| isListItem.test(line)) { | ||
| cm.execCommand('indentMore'); | ||
| } else { | ||
| cm.execCommand('insertSoftTab'); | ||
| } | ||
| } | ||
| }, | ||
| 'Shift-Tab': (cm: any) => cm.execCommand('indentLess') | ||
| }); | ||
| } | ||
| this.setOption('indentWithTabs', newSettings.indentWithTabs); | ||
| this.setOption('indentUnit', newSettings.indentUnit); | ||
| this.setOption('tabSize', newSettings.tabSize); | ||
| this.setOption('smartIndent', newSettings.smartIndent); | ||
| }); | ||
| }, | ||
|
|
||
| codeMirrorOptions: { | ||
| 'enable-space-indenter': true, | ||
| }, | ||
| } | ||
| }; | ||
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,32 @@ | ||
| import getSettings from "./getSettings"; | ||
| import type { PluginContext } from "./types"; | ||
| import { Prec, EditorState } from "@codemirror/state"; | ||
| import { indentService, indentUnit } from "@codemirror/language"; | ||
|
|
||
| export default function(context: PluginContext) { | ||
| return { | ||
| plugin: async function plugin(CodeMirror: any) { | ||
| if (!CodeMirror.cm6) return; | ||
|
|
||
| const settings = await getSettings(context); | ||
|
|
||
| // CodeMirror 6 uses a string indent unit | ||
| let indentation = ' '.repeat(settings.indentUnit); | ||
| if (settings.indentWithTabs) { | ||
| indentation = '\t'.repeat(Math.floor(settings.indentUnit / settings.tabSize)); | ||
| } | ||
|
|
||
| // Returning null: Makes the editor use the indentation of the line above the current. | ||
| // See https://codemirror.net/docs/ref/#language.indentService | ||
| const continueIndentIndenter = indentService.of(() => null); | ||
|
|
||
| CodeMirror.addExtension([ | ||
| Prec.high([ | ||
| indentUnit.of(indentation), | ||
| EditorState.tabSize.of(settings.tabSize), | ||
| settings.smartIndent ? [] : continueIndentIndenter, | ||
| ]), | ||
| ]); | ||
| }, | ||
| }; | ||
| }; |
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,28 @@ | ||
| import type { IndentSettings } from "../settings"; | ||
| import { PluginContext } from "./types"; | ||
|
|
||
| export default function getSettings(context: PluginContext) { | ||
| async function get_settings() { | ||
| return await context.postMessage({name: 'getSettings'}); | ||
| } | ||
|
|
||
| return new Promise<IndentSettings>(resolve => { | ||
| // trying to do what Rich Markdown does | ||
| // https://github.com/CalebJohn/joplin-rich-markdown/blob/a32b087fa9d317ae7cf518a4ba79b3bca7556983/src/richMarkdown.ts | ||
| // | ||
| // retry to get settings until success | ||
| async function backoff(timeout: number) { | ||
| const settings = await get_settings(); | ||
|
|
||
| if (!settings) { | ||
| setTimeout(backoff, timeout * 2, timeout * 2); | ||
| } | ||
| else { | ||
| resolve(settings); | ||
| } | ||
| }; | ||
| // Set the first timeout to 50 because settings are usually ready immediately | ||
| // Set the first backoff to (100*2) to give a little extra time | ||
| setTimeout(backoff, 50, 100); | ||
| }); | ||
| } |
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,2 @@ | ||
|
|
||
| export type PluginContext = { postMessage: any }; |
This file was deleted.
Oops, something went wrong.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codeMirror5.tsis a version ofindent.js, migrated to TypeScript and disabled in the CodeMirror 6 (beta markdown) editor.