Skip to content

Commit

Permalink
CloseBrackets setting toggle (#102)
Browse files Browse the repository at this point in the history
* Add a setting to toggle the auto bracket-closing of CodeMirror

* Move Auto-close brackets setting into new settings tab Editing

* Add tests for Auto-close bracket setting

---------

Co-authored-by: Jonatan Heyman <[email protected]>
  • Loading branch information
flolbr and heyman authored Jan 4, 2024
1 parent acb7ddf commit 0f3c714
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
2 changes: 2 additions & 0 deletions electron/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const schema = {
"bufferPath" : {type: "string", default: ""},
"showInDock": {type: "boolean", default: true},
"showInMenu": {type: "boolean", default: false},
"bracketClosing": {type: "boolean", default: false},
},
},

Expand Down Expand Up @@ -59,6 +60,7 @@ const defaults = {
bufferPath: "",
showInDock: true,
showInMenu: false,
bracketClosing: false,
},
theme: "system",
}
Expand Down
1 change: 1 addition & 0 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
:keymap="settings.keymap"
:showLineNumberGutter="settings.showLineNumberGutter"
:showFoldGutter="settings.showFoldGutter"
:bracketClosing="settings.bracketClosing"
class="editor"
ref="editor"
@openLanguageSelector="openLanguageSelector"
Expand Down
9 changes: 9 additions & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
type: Boolean,
default: true,
},
bracketClosing: {
type: Boolean,
default: false,
},
},
components: {},
Expand Down Expand Up @@ -61,6 +65,7 @@
keymap: this.keymap,
showLineNumberGutter: this.showLineNumberGutter,
showFoldGutter: this.showFoldGutter,
bracketClosing: this.bracketClosing,
})
window._heynote_editor = this.editor
window.document.addEventListener("currenciesLoaded", this.onCurrenciesLoaded)
Expand Down Expand Up @@ -116,6 +121,10 @@
showFoldGutter(show) {
this.editor.setFoldGutter(show)
},
bracketClosing(value) {
this.editor.setBracketClosing(value)
},
},
methods: {
Expand Down
34 changes: 29 additions & 5 deletions src/components/settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
globalHotkey: this.initialSettings.globalHotkey,
showInDock: this.initialSettings.showInDock,
showInMenu: this.initialSettings.showInMenu,
bracketClosing: this.initialSettings.bracketClosing,
autoUpdate: this.initialSettings.autoUpdate,
activeTab: "general",
Expand Down Expand Up @@ -64,6 +65,7 @@
showInDock: this.showInDock,
showInMenu: this.showInMenu || !this.showInDock,
autoUpdate: this.autoUpdate,
bracketClosing: this.bracketClosing,
})
},
}
Expand All @@ -83,6 +85,12 @@
:activeTab="activeTab"
@click="activeTab = 'general'"
/>
<TabListItem
name="Editing"
tab="editing"
:activeTab="activeTab"
@click="activeTab = 'editing'"
/>
<TabListItem
name="Appearance"
tab="appearance"
Expand Down Expand Up @@ -140,22 +148,38 @@
<div class="entry">
<h2>Show In</h2>
<label v-if="isMac">
<input
type="checkbox"
v-model="showInDock"
<input
type="checkbox"
v-model="showInDock"
@change="updateSettings"
/>
Show in dock
</label>
<label>
<input
<input
type="checkbox"
:disabled="!showInDock"
v-model="showInMenu"
v-model="showInMenu"
@change="updateSettings"
/>
Show system tray
</label>
</div>
</div>
</TabContent>

<TabContent tab="editing" :activeTab="activeTab">
<div class="row">
<div class="entry">
<h2>Input settings</h2>
<label>
<input
type="checkbox"
v-model="bracketClosing"
@change="updateSettings"
/>
Auto-close brackets and quotation marks
</label>
</div>
</div>
</TabContent>
Expand Down
11 changes: 11 additions & 0 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Annotation, EditorState, Compartment } from "@codemirror/state"
import { EditorView, keymap, drawSelection, ViewPlugin, lineNumbers } from "@codemirror/view"
import { indentUnit, forceParsing, foldGutter } from "@codemirror/language"
import { markdown } from "@codemirror/lang-markdown"
import { closeBrackets } from "@codemirror/autocomplete";

import { heynoteLight } from "./theme/light.js"
import { heynoteDark } from "./theme/dark.js"
Expand Down Expand Up @@ -41,6 +42,7 @@ export class HeynoteEditor {
keymap="default",
showLineNumberGutter=true,
showFoldGutter=true,
bracketClosing=false,
}) {
this.element = element
this.themeCompartment = new Compartment
Expand All @@ -49,6 +51,7 @@ export class HeynoteEditor {
this.lineNumberCompartment = new Compartment
this.foldGutterCompartment = new Compartment
this.readOnlyCompartment = new Compartment
this.closeBracketsCompartment = new Compartment
this.deselectOnCopy = keymap === "emacs"

const state = EditorState.create({
Expand All @@ -62,6 +65,8 @@ export class HeynoteEditor {
customSetup,
this.foldGutterCompartment.of(showFoldGutter ? [foldGutter()] : []),

this.closeBracketsCompartment.of(bracketClosing ? [closeBrackets()] : []),

this.readOnlyCompartment.of([]),

this.themeCompartment.of(theme === "dark" ? heynoteDark : heynoteLight),
Expand Down Expand Up @@ -177,6 +182,12 @@ export class HeynoteEditor {
})
}

setBracketClosing(value) {
this.view.dispatch({
effects: this.closeBracketsCompartment.reconfigure(value ? [closeBrackets()] : []),
})
}

formatCurrentBlock() {
formatBlockContent({
state: this.view.state,
Expand Down
27 changes: 27 additions & 0 deletions tests/auto-closing-brackets.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test, expect } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";

let heynotePage

test.beforeEach(async ({ page }) => {
heynotePage = new HeynotePage(page)
await heynotePage.goto()
});

test("test bracket closing default off", async ({ page }) => {
await page.locator("body").pressSequentially("{")
expect(await heynotePage.getBlockContent(0)).toBe("{")
})

test("test bracket closing", async ({ page }) => {
await page.locator("css=.status-block.settings").click()
await page.locator("css=li.tab-editing").click()
await page.getByLabel("Auto-close brackets and quotation marks").click()
await page.locator("body").press("Escape")
await page.locator("body").pressSequentially("{")
expect(await heynotePage.getBlockContent(0)).toBe("{}")
await page.locator("body").press("Backspace")
expect(await heynotePage.getBlockContent(0)).toBe("")
await page.locator("body").pressSequentially("(hej")
await page.locator("body").pressSequentially("(hej)")
})
1 change: 1 addition & 0 deletions webapp/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ let initialSettings = {
emacsMetaKey: "meta",
showLineNumberGutter: true,
showFoldGutter: true,
bracketClosing: false,
}
if (settingsData !== null) {
initialSettings = Object.assign(initialSettings, JSON.parse(settingsData))
Expand Down

0 comments on commit 0f3c714

Please sign in to comment.