Added option to jump-to-start on file open

closes #1407
This commit is contained in:
Mitch Capper 2022-03-16 15:40:23 -07:00
parent 03afc1ec29
commit f54896f5e7
8 changed files with 32 additions and 11 deletions

View File

@ -11,13 +11,14 @@ Preferences can be controlled and modified in the settings window or via the `pr
| titleBarStyle | String | custom | The title bar style on Linux and Window: `custom` or `native` | | titleBarStyle | String | custom | The title bar style on Linux and Window: `custom` or `native` |
| openFilesInNewWindow | Boolean | false | true, false | | openFilesInNewWindow | Boolean | false | true, false |
| openFolderInNewWindow | Boolean | false | true, false | | openFolderInNewWindow | Boolean | false | true, false |
| zoom | Number | 1.0 | The zoom level. Between 0.5 and 2.0 inclusive. | | zoom | Number | 1.0 | The zoom level. Between 0.5 and 2.0 inclusive. |
| hideScrollbar | Boolean | false | Whether to hide scrollbars. Optional value: true, false | | hideScrollbar | Boolean | false | Whether to hide scrollbars. Optional value: true, false |
| jumpToStartOnLoad | Boolean | false | Jump to the start of the document on load (rather than end) |
| wordWrapInToc | Boolean | false | Whether to enable word wrap in TOC. Optional value: true, false | | wordWrapInToc | Boolean | false | Whether to enable word wrap in TOC. Optional value: true, false |
| fileSortBy | String | created | Sort files in opened folder by `created` time, modified time and title. | | fileSortBy | String | created | Sort files in opened folder by `created` time, modified time and title. |
| startUpAction | String | lastState | The action after MarkText startup, open the last edited content, open the specified folder or blank page, optional value: `lastState`, `folder`, `blank` | | startUpAction | String | lastState | The action after MarkText startup, open the last edited content, open the specified folder or blank page, optional value: `lastState`, `folder`, `blank` |
| defaultDirectoryToOpen | String | `""` | The path that should be opened if `startUpAction=folder`. | | defaultDirectoryToOpen | String | `""` | The path that should be opened if `startUpAction=folder`. |
| language | String | en | The language MarkText use. | | language | String | en | The language MarkText use. |
#### Editor #### Editor

View File

@ -40,6 +40,11 @@
"type": "boolean", "type": "boolean",
"default": false "default": false
}, },
"jumpToStartOnLoad": {
"description": "General--Whether to jump to start (vs end) of document on load.",
"type": "boolean",
"default": false
},
"wordWrapInToc": { "wordWrapInToc": {
"description": "General--Whether to enable word wrap in TOC.", "description": "General--Whether to enable word wrap in TOC.",
"type": "boolean", "type": "boolean",

View File

@ -165,7 +165,7 @@ class Muya {
return this.contentState.getCodeMirrorCursor() return this.contentState.getCodeMirrorCursor()
} }
setMarkdown (markdown, cursor, isRenderCursor = true) { setMarkdown (markdown, cursor, isRenderCursor = true, setToStartNotEndIfNoCursor = false) {
let newMarkdown = markdown let newMarkdown = markdown
let isValid = false let isValid = false
if (cursor && cursor.anchor && cursor.focus) { if (cursor && cursor.anchor && cursor.focus) {
@ -174,7 +174,7 @@ class Muya {
isValid = cursorInfo.isValid isValid = cursorInfo.isValid
} }
this.contentState.importMarkdown(newMarkdown) this.contentState.importMarkdown(newMarkdown)
this.contentState.importCursor(cursor && isValid) this.contentState.importCursor(cursor && isValid, setToStartNotEndIfNoCursor)
this.contentState.render(isRenderCursor) this.contentState.render(isRenderCursor)
setTimeout(() => { setTimeout(() => {
this.dispatchChange() this.dispatchChange()

View File

@ -540,7 +540,7 @@ const importRegister = ContentState => {
} }
} }
ContentState.prototype.importCursor = function (hasCursor) { ContentState.prototype.importCursor = function (hasCursor, setToStartNotEndIfNoCursor = false) {
// set cursor // set cursor
const cursor = { const cursor = {
anchor: null, anchor: null,
@ -581,9 +581,9 @@ const importRegister = ContentState => {
if (hasCursor) { if (hasCursor) {
travel(this.blocks) travel(this.blocks)
} else { } else {
const lastBlock = this.getLastBlock() const jumpBlock = setToStartNotEndIfNoCursor ? this.getFirstBlock() : this.getLastBlock()
const key = lastBlock.key const key = jumpBlock.key
const offset = lastBlock.text.length const offset = setToStartNotEndIfNoCursor ? 0 : jumpBlock.text.length
cursor.anchor = { key, offset } cursor.anchor = { key, offset }
cursor.focus = { key, offset } cursor.focus = { key, offset }
} }

View File

@ -162,6 +162,7 @@ export default {
theme: state => state.preferences.theme, theme: state => state.preferences.theme,
sequenceTheme: state => state.preferences.sequenceTheme, sequenceTheme: state => state.preferences.sequenceTheme,
hideScrollbar: state => state.preferences.hideScrollbar, hideScrollbar: state => state.preferences.hideScrollbar,
jumpToStartOnLoad: state => state.preferences.jumpToStartOnLoad,
spellcheckerEnabled: state => state.preferences.spellcheckerEnabled, spellcheckerEnabled: state => state.preferences.spellcheckerEnabled,
spellcheckerIsHunspell: state => state.preferences.spellcheckerIsHunspell, spellcheckerIsHunspell: state => state.preferences.spellcheckerIsHunspell,
spellcheckerNoUnderline: state => state.preferences.spellcheckerNoUnderline, spellcheckerNoUnderline: state => state.preferences.spellcheckerNoUnderline,
@ -410,6 +411,12 @@ export default {
}) })
} }
}, },
jumpToStartOnLoad: function (value, oldValue) {
const { editor } = this
if (value !== oldValue && editor) {
editor.setOptions({ jumpToStartOnLoad: value })
}
},
spellcheckerEnabled: function (value, oldValue) { spellcheckerEnabled: function (value, oldValue) {
if (value !== oldValue) { if (value !== oldValue) {
@ -1229,7 +1236,7 @@ export default {
if (cursor) { if (cursor) {
editor.setMarkdown(markdown, cursor, true) editor.setMarkdown(markdown, cursor, true)
} else { } else {
editor.setMarkdown(markdown) editor.setMarkdown(markdown, null, true, this.jumpToStartOnLoad)
} }
} }
}, },
@ -1243,7 +1250,7 @@ export default {
editor.setHistory(history) editor.setHistory(history)
} }
if (typeof markdown === 'string') { if (typeof markdown === 'string') {
editor.setMarkdown(markdown, cursor, renderCursor) editor.setMarkdown(markdown, cursor, renderCursor, this.jumpToStartOnLoad)
} else if (cursor) { } else if (cursor) {
editor.setCursor(cursor) editor.setCursor(cursor)
} }

View File

@ -40,6 +40,11 @@
description="Hide scrollbars" description="Hide scrollbars"
:bool="hideScrollbar" :bool="hideScrollbar"
:onChange="value => onSelectChange('hideScrollbar', value)" :onChange="value => onSelectChange('hideScrollbar', value)"
></bool>
<bool
description="Jump to start (vs end) on load"
:bool="jumpToStartOnLoad"
:onChange="value => onSelectChange('jumpToStartOnLoad', value)"
></bool> ></bool>
<bool <bool
description="Open files in new window" description="Open files in new window"
@ -158,6 +163,7 @@ export default {
defaultDirectoryToOpen: state => state.preferences.defaultDirectoryToOpen, defaultDirectoryToOpen: state => state.preferences.defaultDirectoryToOpen,
openFilesInNewWindow: state => state.preferences.openFilesInNewWindow, openFilesInNewWindow: state => state.preferences.openFilesInNewWindow,
openFolderInNewWindow: state => state.preferences.openFolderInNewWindow, openFolderInNewWindow: state => state.preferences.openFolderInNewWindow,
jumpToStartOnLoad: state => state.preferences.jumpToStartOnLoad,
zoom: state => state.preferences.zoom, zoom: state => state.preferences.zoom,
hideScrollbar: state => state.preferences.hideScrollbar, hideScrollbar: state => state.preferences.hideScrollbar,
wordWrapInToc: state => state.preferences.wordWrapInToc, wordWrapInToc: state => state.preferences.wordWrapInToc,

View File

@ -10,6 +10,7 @@ const state = {
openFolderInNewWindow: false, openFolderInNewWindow: false,
zoom: 1.0, zoom: 1.0,
hideScrollbar: false, hideScrollbar: false,
jumpToStartOnLoad: false,
wordWrapInToc: false, wordWrapInToc: false,
fileSortBy: 'created', fileSortBy: 'created',
startUpAction: 'lastState', startUpAction: 'lastState',

View File

@ -6,6 +6,7 @@
"openFolderInNewWindow": false, "openFolderInNewWindow": false,
"zoom": 1.0, "zoom": 1.0,
"hideScrollbar": false, "hideScrollbar": false,
"jumpToStartOnLoad": false,
"wordWrapInToc": false, "wordWrapInToc": false,
"fileSortBy": "created", "fileSortBy": "created",
"startUpAction": "lastState", "startUpAction": "lastState",