Add select all command that first selects the current note, and then the whole document (if executed with a whole note selected)

This commit is contained in:
Jonatan Heyman 2022-12-30 15:31:12 +01:00
parent 4307bf6d0f
commit 0f072b8699
3 changed files with 28 additions and 2 deletions

View File

@ -1,4 +1,6 @@
import { EditorView } from "@codemirror/view" import { EditorView } from "@codemirror/view"
import { selectAll as defaultSelectAll } from "@codemirror/commands"
import { blockState } from "./note-block"
export const insertNewNote = ({ state, dispatch }) => { export const insertNewNote = ({ state, dispatch }) => {
@ -16,3 +18,22 @@ export const insertNewNote = ({ state, dispatch }) => {
return true; return true;
} }
export const selectAll = ({ state, dispatch }) => {
// find which block the cursor is in
const range = state.selection.asSingle().ranges[0]
const block = state.facet(blockState).find(block => block.content.from <= range.from && block.content.to >= range.from)
// check if all the text of the note is already selected, in which case we want to select all the text of the whole document
if (range.from === block.content.from && range.to === block.content.to) {
return defaultSelectAll({state, dispatch})
}
dispatch(state.update({
selection: {anchor: block.content.from, head: block.content.to},
userEvent: "select"
}))
return true
}

View File

@ -39,7 +39,7 @@ function getBlocks(state) {
return blocks return blocks
} }
const blockState = StateField.define({ export const blockState = StateField.define({
create(state) { create(state) {
return getBlocks(state); return getBlocks(state);
}, },

View File

@ -7,7 +7,7 @@ import { nord } from "./theme/nord.mjs"
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 } from "./block/note-block.js" import { noteBlockExtension } from "./block/note-block.js"
import { insertNewNote } from "./block/commands.js"; import { insertNewNote, selectAll } from "./block/commands.js";
export class HeynoteEditor { export class HeynoteEditor {
@ -43,6 +43,11 @@ export class HeynoteEditor {
preventDefault: true, preventDefault: true,
run: insertNewNote, run: insertNewNote,
}, },
{
key: "Mod-a",
preventDefault: true,
run: selectAll,
},
]), ]),
customSetup, customSetup,