Skip to content

Commit

Permalink
Throw error if major format version is greater than the currently sup…
Browse files Browse the repository at this point in the history
…ported version
  • Loading branch information
heyman committed Jul 17, 2024
1 parent 8f39b66 commit 68c741e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 116 deletions.
98 changes: 17 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"vue-tsc": "^1.0.16"
},
"dependencies": {
"electron-log": "^5.0.1"
"electron-log": "^5.0.1",
"semver": "^7.6.3"
}
}
61 changes: 33 additions & 28 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,40 @@
// load buffer content and create editor
window.heynote.buffer.load().then((content) => {
let diskContent = content
this.editor = new HeynoteEditor({
element: this.$refs.editor,
content: content,
theme: this.theme,
saveFunction: (content) => {
if (content === diskContent) {
return
}
try {
let diskContent = content
this.editor = new HeynoteEditor({
element: this.$refs.editor,
content: content,
theme: this.theme,
saveFunction: (content) => {
if (content === diskContent) {
return
}
diskContent = content
window.heynote.buffer.save(content)
},
keymap: this.keymap,
emacsMetaKey: this.emacsMetaKey,
showLineNumberGutter: this.showLineNumberGutter,
showFoldGutter: this.showFoldGutter,
bracketClosing: this.bracketClosing,
fontFamily: this.fontFamily,
fontSize: this.fontSize,
})
window._heynote_editor = this.editor
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)
this.editor.setDefaultBlockLanguage(this.defaultBlockLanguage, this.defaultBlockLanguageAutoDetect)
// set up buffer change listener
window.heynote.buffer.onChangeCallback((event, content) => {
diskContent = content
window.heynote.buffer.save(content)
},
keymap: this.keymap,
emacsMetaKey: this.emacsMetaKey,
showLineNumberGutter: this.showLineNumberGutter,
showFoldGutter: this.showFoldGutter,
bracketClosing: this.bracketClosing,
fontFamily: this.fontFamily,
fontSize: this.fontSize,
})
window._heynote_editor = this.editor
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)
this.editor.setDefaultBlockLanguage(this.defaultBlockLanguage, this.defaultBlockLanguageAutoDetect)
// set up buffer change listener
window.heynote.buffer.onChangeCallback((event, content) => {
diskContent = content
this.editor.setContent(content)
})
this.editor.setContent(content)
})
} catch (e) {
alert("Error! " + e.message)
throw e
}
})
// set up window close handler that will save the buffer and quit
window.heynote.onWindowClose(() => {
Expand Down
13 changes: 9 additions & 4 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ export class HeynoteEditor {
state: state,
parent: element,
})

this.setContent(content)

if (focus) {
this.view.focus()
}
Expand All @@ -136,9 +136,14 @@ export class HeynoteEditor {
}

setContent(content) {
return new Promise((resolve) => {
try {
this.note = NoteFormat.load(content)

this.setReadOnly(false)
} catch (e) {
this.setReadOnly(true)
throw e
}
return new Promise((resolve) => {
// set buffer content
this.view.dispatch({
changes: {
Expand Down
14 changes: 12 additions & 2 deletions src/editor/note-format.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { major } from "semver";


const FORMAT_VERSION = "1.0.0"


export class NoteFormat {
constructor() {
this.content = '';
this.metadata = {};
this.metadata = {formatVersion: "0.0.0"};
}

static load(data) {
Expand All @@ -16,12 +22,16 @@ export class NoteFormat {
}
note.content = data.slice(firstSeparator)
}

if (major(note.metadata.formatVersion) > major(FORMAT_VERSION)) {
throw new Error(`Unsupported Heynote format version: ${note.metadata.formatVersion}. You probably need to update Heynote.`)
}

return note
}

serialize() {
this.metadata.formatVersion = "1.0"
this.metadata.formatVersion = FORMAT_VERSION
return JSON.stringify(this.metadata) + this.content
}

Expand Down

0 comments on commit 68c741e

Please sign in to comment.