Refresh Math blocks when new exchange rates are loaded

This commit is contained in:
Jonatan Heyman 2023-07-04 14:01:59 +02:00
parent 182c6f295e
commit 0c567384ee
8 changed files with 42 additions and 5 deletions

View File

@ -27,6 +27,7 @@ Available for Mac and Windows. Linux support coming soon (hopefully).
- Language auto-detection - Language auto-detection
- Auto-formatting - Auto-formatting
- Math/Calculator mode - Math/Calculator mode
- Currency conversion
- Multi-cursor editing - Multi-cursor editing
- Dark & Light themes - Dark & Light themes
- Default or Emacs-like key bindings - Default or Emacs-like key bindings

View File

@ -22,11 +22,13 @@ radius = 5
volume = radius^2 * PI volume = radius^2 * PI
sqrt(9) sqrt(9)
It also supports some basic unit conversions: It also supports some basic unit conversions, including currencies:
13 inches in cm 13 inches in cm
time = 3900 seconds to minutes time = 3900 seconds to minutes
time * 2 time * 2
1 EUR in USD
markdown markdown
In Markdown blocks, lists with [x] and [ ] are rendered as checkboxes: In Markdown blocks, lists with [x] and [ ] are rendered as checkboxes:
@ -119,4 +121,5 @@ Shopping list:
- Milk - Milk
- Eggs - Eggs
- Bread - Bread
- Cheese` - Cheese`

View File

@ -54,6 +54,7 @@
showLineNumberGutter: this.showLineNumberGutter, showLineNumberGutter: this.showLineNumberGutter,
showFoldGutter: this.showFoldGutter, showFoldGutter: this.showFoldGutter,
}) })
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)
}) })
// set up window close handler that will save the buffer and quit // set up window close handler that will save the buffer and quit
window.heynote.onWindowClose(() => { window.heynote.onWindowClose(() => {
@ -80,6 +81,10 @@
} }
}, },
beforeUnmount() {
window.document.removeEventListener("currenciesLoaded", this.onCurrenciesLoaded)
},
watch: { watch: {
theme(newTheme) { theme(newTheme) {
this.editor.setTheme(newTheme) this.editor.setTheme(newTheme)
@ -113,6 +118,10 @@
this.editor.focus() this.editor.focus()
}, },
onCurrenciesLoaded() {
this.editor.currenciesLoaded()
},
focus() { focus() {
this.editor.focus() this.editor.focus()
}, },

View File

@ -14,4 +14,5 @@ export async function loadCurrencies() {
}, {override: currenciesLoaded}) }, {override: currenciesLoaded})
}) })
currenciesLoaded = true currenciesLoaded = true
window.document.dispatchEvent(new Event("currenciesLoaded"))
} }

View File

@ -2,4 +2,5 @@ import { Annotation } from "@codemirror/state"
export const heynoteEvent = Annotation.define() export const heynoteEvent = Annotation.define()
export const LANGUAGE_CHANGE = "heynote-change" export const LANGUAGE_CHANGE = "heynote-change"
export const CURRENCIES_LOADED = "heynote-currencies-loaded"

View File

@ -4,7 +4,7 @@ import {
selectAll as defaultSelectAll, selectAll as defaultSelectAll,
moveLineUp as defaultMoveLineUp, moveLineUp as defaultMoveLineUp,
} from "@codemirror/commands" } from "@codemirror/commands"
import { heynoteEvent, LANGUAGE_CHANGE } from "../annotation.js"; import { heynoteEvent, LANGUAGE_CHANGE, CURRENCIES_LOADED } from "../annotation.js";
import { blockState, getActiveNoteBlock, getNoteBlockFromPos } from "./block" import { blockState, getActiveNoteBlock, getNoteBlockFromPos } from "./block"
import { levenshtein_distance } from "../language-detection/levenshtein" import { levenshtein_distance } from "../language-detection/levenshtein"
import { moveLineDown, moveLineUp } from "./move-lines.js"; import { moveLineDown, moveLineUp } from "./move-lines.js";
@ -277,3 +277,12 @@ export function newCursorBelow(view) {
export function newCursorAbove(view) { export function newCursorAbove(view) {
newCursor(view, false) newCursor(view, false)
} }
export function triggerCurrenciesLoaded(state, dispatch) {
// Trigger empty change transaction that is annotated with CURRENCIES_LOADED
// This will make Math blocks re-render so that currency conversions are applied
dispatch(state.update({
changes:{from: 0, to: 0, insert:""},
annotations: [heynoteEvent.of(CURRENCIES_LOADED)],
}))
}

View File

@ -4,6 +4,7 @@ import { RangeSetBuilder } from "@codemirror/state"
import { WidgetType } from "@codemirror/view" import { WidgetType } from "@codemirror/view"
import { getNoteBlockFromPos } from "./block" import { getNoteBlockFromPos } from "./block"
import { CURRENCIES_LOADED } from "../annotation"
class MathResult extends WidgetType { class MathResult extends WidgetType {
@ -84,6 +85,11 @@ function mathDeco(view) {
} }
// This function checks if any of the transactions has the given annotation
const transactionsHasAnnotation = (transactions, annotation) => {
return transactions.some(tr => tr.annotations.some(a => a.value === annotation))
}
export const mathBlock = ViewPlugin.fromClass(class { export const mathBlock = ViewPlugin.fromClass(class {
decorations decorations
@ -92,7 +98,10 @@ export const mathBlock = ViewPlugin.fromClass(class {
} }
update(update) { update(update) {
if (update.docChanged || update.viewportChanged) { // If the document changed, the viewport changed, or the transaction was annotated with the CURRENCIES_LOADED annotation,
// update the decorations. The reason we need to check for CURRENCIES_LOADED annotations is because the currency rates are
// updated asynchronously
if (update.docChanged || update.viewportChanged || transactionsHasAnnotation(update.transactions, CURRENCIES_LOADED)) {
this.decorations = mathDeco(update.view) this.decorations = mathDeco(update.view)
} }
} }

View File

@ -9,7 +9,7 @@ import { heynoteBase } from "./theme/base.js"
import { customSetup } from "./setup.js" import { customSetup } from "./setup.js"
import { heynoteLang } from "./lang-heynote/heynote.js" import { heynoteLang } from "./lang-heynote/heynote.js"
import { noteBlockExtension, blockLineNumbers } from "./block/block.js" import { noteBlockExtension, blockLineNumbers } from "./block/block.js"
import { changeCurrentBlockLanguage } from "./block/commands.js" import { changeCurrentBlockLanguage, triggerCurrenciesLoaded } from "./block/commands.js"
import { formatBlockContent } from "./block/format-code.js" import { formatBlockContent } from "./block/format-code.js"
import { heynoteKeymap } from "./keymap.js" import { heynoteKeymap } from "./keymap.js"
import { emacsKeymap } from "./emacs.js" import { emacsKeymap } from "./emacs.js"
@ -147,6 +147,10 @@ export class HeynoteEditor {
dispatch: this.view.dispatch, dispatch: this.view.dispatch,
}) })
} }
currenciesLoaded() {
triggerCurrenciesLoaded(this.view.state, this.view.dispatch)
}
} }