Add a default language setting

This commit introduces the ability to set and use a default programming language for new editor blocks. Key changes include:
- Adding a sorted list of languages and a `defaultLanguage` setting in the configuration.
- Propagating the `defaultLanguage` setting to relevant components and functions.
- Enabling users to select and update the default language in the settings UI.
This commit is contained in:
Florian Labarre 2024-01-02 15:09:11 +01:00
parent 0ba5820cf4
commit 084ba28cb8
6 changed files with 75 additions and 2 deletions

View File

@ -1,5 +1,36 @@
import Store from "electron-store" import Store from "electron-store"
let sortedLanguages = [
"csharp",
"cpp",
"clojure",
"css",
"erlang",
"golang",
"html",
"java",
"javascript",
"json",
"jsx",
"kotlin",
"lezer",
"markdown",
"math",
"php",
"text",
"python",
"ruby",
"rust",
"shell",
"sql",
"swift",
"toml",
"tsx",
"typescript",
"xml",
"yaml",
]
const isDev = !!process.env.VITE_DEV_SERVER_URL const isDev = !!process.env.VITE_DEV_SERVER_URL
const schema = { const schema = {
@ -32,6 +63,7 @@ const schema = {
"bufferPath" : {type: "string", default: ""}, "bufferPath" : {type: "string", default: ""},
"showInDock": {type: "boolean", default: true}, "showInDock": {type: "boolean", default: true},
"showInMenu": {type: "boolean", default: false}, "showInMenu": {type: "boolean", default: false},
"defaultLanguage": { "enum": sortedLanguages, default: "text" },
}, },
}, },
@ -59,6 +91,7 @@ const defaults = {
bufferPath: "", bufferPath: "",
showInDock: true, showInDock: true,
showInMenu: false, showInMenu: false,
defaultLanguage: "text"
}, },
theme: "system", theme: "system",
} }

View File

@ -118,6 +118,7 @@
:keymap="settings.keymap" :keymap="settings.keymap"
:showLineNumberGutter="settings.showLineNumberGutter" :showLineNumberGutter="settings.showLineNumberGutter"
:showFoldGutter="settings.showFoldGutter" :showFoldGutter="settings.showFoldGutter"
:defaultLanguage="settings.defaultLanguage"
class="editor" class="editor"
ref="editor" ref="editor"
@openLanguageSelector="openLanguageSelector" @openLanguageSelector="openLanguageSelector"

View File

@ -19,6 +19,10 @@
type: Boolean, type: Boolean,
default: true, default: true,
}, },
defaultLanguage: {
type: String,
default: "text",
},
}, },
components: {}, components: {},
@ -61,6 +65,7 @@
keymap: this.keymap, keymap: this.keymap,
showLineNumberGutter: this.showLineNumberGutter, showLineNumberGutter: this.showLineNumberGutter,
showFoldGutter: this.showFoldGutter, showFoldGutter: this.showFoldGutter,
defaultLanguage: this.defaultLanguage,
}) })
window._heynote_editor = this.editor window._heynote_editor = this.editor
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded) window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)

View File

@ -2,6 +2,8 @@
import KeyboardHotkey from "./KeyboardHotkey.vue" import KeyboardHotkey from "./KeyboardHotkey.vue"
import TabListItem from "./TabListItem.vue" import TabListItem from "./TabListItem.vue"
import TabContent from "./TabContent.vue" import TabContent from "./TabContent.vue"
import { sortedLanguages } from "@/src/editor/languages";
import { setNewBlockLanguage } from "@/src/editor/block/commands";
export default { export default {
props: { props: {
@ -30,7 +32,9 @@
globalHotkey: this.initialSettings.globalHotkey, globalHotkey: this.initialSettings.globalHotkey,
showInDock: this.initialSettings.showInDock, showInDock: this.initialSettings.showInDock,
showInMenu: this.initialSettings.showInMenu, showInMenu: this.initialSettings.showInMenu,
defaultLanguage: this.initialSettings.defaultLanguage,
autoUpdate: this.initialSettings.autoUpdate, autoUpdate: this.initialSettings.autoUpdate,
languages: sortedLanguages,
activeTab: "general", activeTab: "general",
} }
@ -63,7 +67,9 @@
showInDock: this.showInDock, showInDock: this.showInDock,
showInMenu: this.showInMenu || !this.showInDock, showInMenu: this.showInMenu || !this.showInDock,
autoUpdate: this.autoUpdate, autoUpdate: this.autoUpdate,
defaultLanguage: this.defaultLanguage,
}) })
setNewBlockLanguage(this.defaultLanguage)
}, },
} }
} }
@ -134,6 +140,19 @@
/> />
</div> </div>
</div> </div>
<div class="row">
<div class="entry">
<h2>Input settings</h2>
<label>
Default Language
<select ref="defaultLanguageSelector" v-model="defaultLanguage" @change="updateSettings">
<template v-for="lg in languages" :key="lg.token">
<option :selected="lg.token === defaultLanguage" :value="lg.token">{{ lg.name }}</option>
</template>
</select>
</label>
</div>
</div>
<div class="row"> <div class="row">
<div class="entry"> <div class="entry">
<h2>Show In</h2> <h2>Show In</h2>

View File

@ -6,6 +6,12 @@ import { selectAll } from "./select-all.js";
export { moveLineDown, moveLineUp, selectAll } export { moveLineDown, moveLineUp, selectAll }
let newBlockLanguage = window.heynote.settings.defaultLanguage || "text"
export const setNewBlockLanguage = (language) => {
newBlockLanguage = language
}
export const insertNewBlockAtCursor = ({ state, dispatch }) => { export const insertNewBlockAtCursor = ({ state, dispatch }) => {
if (state.readOnly) if (state.readOnly)
@ -16,7 +22,7 @@ export const insertNewBlockAtCursor = ({ state, dispatch }) => {
if (currentBlock) { if (currentBlock) {
delimText = `\n∞∞∞${currentBlock.language.name}${currentBlock.language.auto ? "-a" : ""}\n` delimText = `\n∞∞∞${currentBlock.language.name}${currentBlock.language.auto ? "-a" : ""}\n`
} else { } else {
delimText = "\n∞∞∞text-a\n" delimText = `\n∞∞∞${newBlockLanguage}-a\n`
} }
dispatch(state.replaceSelection(delimText), dispatch(state.replaceSelection(delimText),
{ {
@ -32,7 +38,7 @@ export const addNewBlockAfterCurrent = ({ state, dispatch }) => {
if (state.readOnly) if (state.readOnly)
return false return false
const block = getActiveNoteBlock(state) const block = getActiveNoteBlock(state)
const delimText = "\n∞∞∞text-a\n" const delimText = `\n∞∞∞${newBlockLanguage}-a\n`
dispatch(state.update({ dispatch(state.update({
changes: { changes: {

View File

@ -237,6 +237,15 @@ export const LANGUAGES = [
const languageMapping = Object.fromEntries(LANGUAGES.map(l => [l.token, l])) const languageMapping = Object.fromEntries(LANGUAGES.map(l => [l.token, l]))
export const sortedLanguages = LANGUAGES.map(l => {
return {
"token": l.token,
"name": l.name
}
}).sort((a, b) => {
return a.name.localeCompare(b.name)
})
export function getLanguage(token) { export function getLanguage(token) {
return languageMapping[token] return languageMapping[token]
} }