From fab1c62fde121e28acd6f49cdf4c56c1443dcbd3 Mon Sep 17 00:00:00 2001 From: Ran Luo Date: Sat, 5 Oct 2019 20:54:34 +0800 Subject: [PATCH] Fix issue 1378 (#1405) * Feat: add trimUnnecessaryEmptyLine setting option * remove debug code * Fix: tpro and remove debug codes * rename trimUnnecessaryEmptyLine to trimUnnecessaryCodeBlockEmptyLines --- docs/PREFERENCES.md | 29 ++++++++++--------- src/main/preferences/schema.json | 5 +++- src/muya/lib/config/index.js | 2 ++ src/muya/lib/utils/importMarkdown.js | 10 ++++--- .../components/editorWithTabs/editor.vue | 9 ++++++ src/renderer/prefComponents/editor/index.vue | 24 +++++++++------ src/renderer/store/preferences.js | 1 + static/preference.json | 1 + 8 files changed, 53 insertions(+), 28 deletions(-) diff --git a/docs/PREFERENCES.md b/docs/PREFERENCES.md index 804d942c..ae812759 100644 --- a/docs/PREFERENCES.md +++ b/docs/PREFERENCES.md @@ -19,20 +19,21 @@ Preferences can be controlled and modified in the settings window or via the `pr #### Editor -| Key | Type | Defaut | Description | -| ---------------------- | ------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| fontSize | Number | 16 | Font size in pixels. 12 ~ 32 | -| editorFontFamily | String | Open Sans | Font Family | -| lineHeight | Number | 1.6 | Line Height | -| autoPairBracket | Boolean | true | Automatically brackets when editing | -| autoPairMarkdownSyntax | Boolean | true | Autocomplete markdown syntax | -| autoPairQuote | Boolean | true | Automatic completion of quotes | -| endOfLine | String | default | The newline character used at the end of each line. The default value is default, which will be selected according to your system intelligence. `lf` `crlf` `default` | -| textDirection | String | ltr | The writing text direction, optional value: `ltr` or `rtl` | -| codeFontSize | Number | 14 | Font size on code block, the range is 12 ~ 28 | -| codeFontFamily | String | `DejaVu Sans Mono` | Code font family | -| hideQuickInsertHint | Boolean | false | Hide hint for quickly creating paragraphs | -| imageDropAction | String | folder | The default behavior after paste or drag the image to Mark Text, upload it to the image cloud (if configured), move to the specified folder, insert the path | +| Key | Type | Defaut | Description | +| ------------------------ | ------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| fontSize | Number | 16 | Font size in pixels. 12 ~ 32 | +| editorFontFamily | String | Open Sans | Font Family | +| lineHeight | Number | 1.6 | Line Height | +| autoPairBracket | Boolean | true | Automatically brackets when editing | +| autoPairMarkdownSyntax | Boolean | true | Autocomplete markdown syntax | +| autoPairQuote | Boolean | true | Automatic completion of quotes | +| endOfLine | String | default | The newline character used at the end of each line. The default value is default, which will be selected according to your system intelligence. `lf` `crlf` `default` | +| textDirection | String | ltr | The writing text direction, optional value: `ltr` or `rtl` | +| codeFontSize | Number | 14 | Font size on code block, the range is 12 ~ 28 | +| codeFontFamily | String | `DejaVu Sans Mono` | Code font family | +| trimUnnecessaryCodeBlockEmptyLines | Boolean | true | Whether to trim the beginning and end empty line in Code block | +| hideQuickInsertHint | Boolean | false | Hide hint for quickly creating paragraphs | +| imageDropAction | String | folder | The default behavior after paste or drag the image to Mark Text, upload it to the image cloud (if configured), move to the specified folder, insert the path | #### Markdown diff --git a/src/main/preferences/schema.json b/src/main/preferences/schema.json index 81e60127..5946c2bc 100644 --- a/src/main/preferences/schema.json +++ b/src/main/preferences/schema.json @@ -92,7 +92,10 @@ "type": "string", "pattern": "^[_A-z0-9]+((-|\\s)*[_A-z0-9])*$" }, - + "trimUnnecessaryCodeBlockEmptyLines": { + "description": "Editor--Trim the beginning and ending empty lines in code block", + "type": "boolean" + }, "autoPairBracket": { "description": "Editor--Automatically brackets when editing", "type": "boolean" diff --git a/src/muya/lib/config/index.js b/src/muya/lib/config/index.js index 0d113c29..a7303810 100644 --- a/src/muya/lib/config/index.js +++ b/src/muya/lib/config/index.js @@ -242,6 +242,8 @@ export const EXPORT_DOMPURIFY_CONFIG = { export const MUYA_DEFAULT_OPTION = { focusMode: false, markdown: '', + // Whether to trim the beginning and ending empty line in code block when open markdown. + trimUnnecessaryCodeBlockEmptyLines: false, preferLooseListItem: true, autoPairBracket: true, autoPairMarkdownSyntax: true, diff --git a/src/muya/lib/utils/importMarkdown.js b/src/muya/lib/utils/importMarkdown.js index 48468eb4..961b8554 100644 --- a/src/muya/lib/utils/importMarkdown.js +++ b/src/muya/lib/utils/importMarkdown.js @@ -76,6 +76,7 @@ const importRegister = ContentState => { nextSibling: null, children: [] } + const { trimUnnecessaryCodeBlockEmptyLines } = this.muya.options const tokens = new Lexer({ disableInline: true }).lex(markdown) let token let block @@ -158,10 +159,11 @@ const importRegister = ContentState => { const lang = (infostring || '').match(/\S*/)[0] value = text - // Fix: #1265 and remove codes bellow. - // if (value.endsWith('\n')) { - // value = value.replace(/\n+$/, '') - // } + // Fix: #1265. + if (trimUnnecessaryCodeBlockEmptyLines && (value.endsWith('\n') || value.startsWith('\n'))) { + value = value.replace(/\n+$/, '') + .replace(/^\n+/, '') + } if (/mermaid|flowchart|vega-lite|sequence/.test(lang)) { block = this.createContainerBlock(lang, value) this.appendChild(parentList[0], block) diff --git a/src/renderer/components/editorWithTabs/editor.vue b/src/renderer/components/editorWithTabs/editor.vue index bce2f5bc..a8f21c99 100644 --- a/src/renderer/components/editorWithTabs/editor.vue +++ b/src/renderer/components/editorWithTabs/editor.vue @@ -138,6 +138,7 @@ export default { fontSize: state => state.preferences.fontSize, codeFontSize: state => state.preferences.codeFontSize, codeFontFamily: state => state.preferences.codeFontFamily, + trimUnnecessaryCodeBlockEmptyLines: state => state.preferences.trimUnnecessaryCodeBlockEmptyLines, editorFontFamily: state => state.preferences.editorFontFamily, hideQuickInsertHint: state => state.preferences.hideQuickInsertHint, editorLineWidth: state => state.preferences.editorLineWidth, @@ -262,6 +263,12 @@ export default { editor.setOptions({ autoPairQuote: value }) } }, + trimUnnecessaryCodeBlockEmptyLines: function (value, oldValue) { + const { editor } = this + if (value !== oldValue && editor) { + editor.setOptions({ trimUnnecessaryCodeBlockEmptyLines: value }) + } + }, bulletListMarker: function (value, oldValue) { const { editor } = this if (value !== oldValue && editor) { @@ -326,6 +333,7 @@ export default { autoPairBracket, autoPairMarkdownSyntax, autoPairQuote, + trimUnnecessaryCodeBlockEmptyLines, bulletListMarker, orderListDelimiter, tabSize, @@ -361,6 +369,7 @@ export default { preferLooseListItem, autoPairBracket, autoPairMarkdownSyntax, + trimUnnecessaryCodeBlockEmptyLines, autoPairQuote, bulletListMarker, orderListDelimiter, diff --git a/src/renderer/prefComponents/editor/index.vue b/src/renderer/prefComponents/editor/index.vue index d7b89d7a..7e439bb4 100644 --- a/src/renderer/prefComponents/editor/index.vue +++ b/src/renderer/prefComponents/editor/index.vue @@ -10,11 +10,20 @@ :step="1" :onChange="value => onSelectChange('fontSize', value)" > + + - + - state.preferences.textDirection, codeFontSize: state => state.preferences.codeFontSize, codeFontFamily: state => state.preferences.codeFontFamily, + trimUnnecessaryCodeBlockEmptyLines: state => state.preferences.trimUnnecessaryCodeBlockEmptyLines, hideQuickInsertHint: state => state.preferences.hideQuickInsertHint, editorLineWidth: state => state.preferences.editorLineWidth }) diff --git a/src/renderer/store/preferences.js b/src/renderer/store/preferences.js index 1fbc1735..aefbc9af 100644 --- a/src/renderer/store/preferences.js +++ b/src/renderer/store/preferences.js @@ -19,6 +19,7 @@ const state = { lineHeight: 1.6, codeFontSize: 14, codeFontFamily: 'DejaVu Sans Mono', + trimUnnecessaryCodeBlockEmptyLines: true, editorLineWidth: '', autoPairBracket: true, diff --git a/static/preference.json b/static/preference.json index 7a596ec2..5095e07e 100644 --- a/static/preference.json +++ b/static/preference.json @@ -16,6 +16,7 @@ "lineHeight": 1.6, "codeFontSize": 14, "codeFontFamily": "DejaVu Sans Mono", + "trimUnnecessaryCodeBlockEmptyLines": true, "editorLineWidth": "", "autoPairBracket": true,