mirror of
https://github.com/heyman/heynote.git
synced 2025-06-25 03:51:26 +02:00
Add selection size to status bar when there are selections
This commit is contained in:
parent
b85ea3023e
commit
cfb95540df
@ -16,6 +16,7 @@
|
|||||||
return {
|
return {
|
||||||
line: 1,
|
line: 1,
|
||||||
column: 1,
|
column: 1,
|
||||||
|
selectionSize: 0,
|
||||||
language: "plaintext",
|
language: "plaintext",
|
||||||
languageAuto: true,
|
languageAuto: true,
|
||||||
theme: window.darkMode.initial,
|
theme: window.darkMode.initial,
|
||||||
@ -71,9 +72,9 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
onCursorChange(e) {
|
onCursorChange(e) {
|
||||||
//console.log("onCursorChange:", e)
|
|
||||||
this.line = e.cursorLine.line
|
this.line = e.cursorLine.line
|
||||||
this.column = e.cursorLine.col
|
this.column = e.cursorLine.col
|
||||||
|
this.selectionSize = e.selectionSize
|
||||||
this.language = e.language
|
this.language = e.language
|
||||||
this.languageAuto = e.languageAuto
|
this.languageAuto = e.languageAuto
|
||||||
},
|
},
|
||||||
@ -117,6 +118,7 @@
|
|||||||
<StatusBar
|
<StatusBar
|
||||||
:line="line"
|
:line="line"
|
||||||
:column="column"
|
:column="column"
|
||||||
|
:selectionSize="selectionSize"
|
||||||
:language="language"
|
:language="language"
|
||||||
:languageAuto="languageAuto"
|
:languageAuto="languageAuto"
|
||||||
:theme="theme"
|
:theme="theme"
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
//console.log("selectionChange:", e)
|
//console.log("selectionChange:", e)
|
||||||
this.$emit("cursorChange", {
|
this.$emit("cursorChange", {
|
||||||
cursorLine: e.cursorLine,
|
cursorLine: e.cursorLine,
|
||||||
|
selectionSize: e.selectionSize,
|
||||||
language: e.language,
|
language: e.language,
|
||||||
languageAuto: e.languageAuto,
|
languageAuto: e.languageAuto,
|
||||||
})
|
})
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
props: [
|
props: [
|
||||||
"line",
|
"line",
|
||||||
"column",
|
"column",
|
||||||
|
"selectionSize",
|
||||||
"language",
|
"language",
|
||||||
"languageAuto",
|
"languageAuto",
|
||||||
"theme",
|
"theme",
|
||||||
@ -46,6 +47,9 @@
|
|||||||
<div class="status-block line-number">
|
<div class="status-block line-number">
|
||||||
Ln <span class="num">{{ line }}</span>
|
Ln <span class="num">{{ line }}</span>
|
||||||
Col <span class="num">{{ column }}</span>
|
Col <span class="num">{{ column }}</span>
|
||||||
|
<template v-if="selectionSize > 0">
|
||||||
|
Sel <span class="num">{{ selectionSize }}</span>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
<div class="spacer"></div>
|
<div class="spacer"></div>
|
||||||
<div
|
<div
|
||||||
|
@ -295,6 +295,20 @@ export const blockLineNumbers = lineNumbers({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
function getSelectionSize(state, sel) {
|
||||||
|
let count = 0
|
||||||
|
let numBlocks = 0
|
||||||
|
for (const block of state.facet(blockState)) {
|
||||||
|
if (sel.from <= block.range.to && sel.to > block.range.from) {
|
||||||
|
count += Math.min(sel.to, block.content.to) - Math.max(sel.from, block.content.from)
|
||||||
|
numBlocks++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count += (numBlocks - 1) * 2 // add 2 for each block separator
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
const emitCursorChange = (editor) => ViewPlugin.fromClass(
|
const emitCursorChange = (editor) => ViewPlugin.fromClass(
|
||||||
class {
|
class {
|
||||||
update(update) {
|
update(update) {
|
||||||
@ -303,10 +317,16 @@ const emitCursorChange = (editor) => ViewPlugin.fromClass(
|
|||||||
const langChange = update.transactions.some(tr => tr.annotations.some(a => a.value == LANGUAGE_CHANGE))
|
const langChange = update.transactions.some(tr => tr.annotations.some(a => a.value == LANGUAGE_CHANGE))
|
||||||
if (update.selectionSet || langChange) {
|
if (update.selectionSet || langChange) {
|
||||||
const cursorLine = getBlockLineFromPos(update.state, update.state.selection.main.head)
|
const cursorLine = getBlockLineFromPos(update.state, update.state.selection.main.head)
|
||||||
|
|
||||||
|
const selectionSize = update.state.selection.ranges.map(
|
||||||
|
(sel) => getSelectionSize(update.state, sel)
|
||||||
|
).reduce((a, b) => a + b, 0)
|
||||||
|
|
||||||
const block = getActiveNoteBlock(update.state)
|
const block = getActiveNoteBlock(update.state)
|
||||||
if (block && cursorLine) {
|
if (block && cursorLine) {
|
||||||
editor.element.dispatchEvent(new SelectionChangeEvent({
|
editor.element.dispatchEvent(new SelectionChangeEvent({
|
||||||
cursorLine,
|
cursorLine,
|
||||||
|
selectionSize,
|
||||||
language: block.language.name,
|
language: block.language.name,
|
||||||
languageAuto: block.language.auto,
|
languageAuto: block.language.auto,
|
||||||
}))
|
}))
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
export class SelectionChangeEvent extends Event {
|
export class SelectionChangeEvent extends Event {
|
||||||
constructor({cursorLine, language, languageAuto}) {
|
constructor({cursorLine, language, languageAuto, selectionSize}) {
|
||||||
super("selectionChange")
|
super("selectionChange")
|
||||||
this.cursorLine = cursorLine
|
this.cursorLine = cursorLine
|
||||||
|
this.selectionSize = selectionSize
|
||||||
this.language = language
|
this.language = language
|
||||||
this.languageAuto = languageAuto
|
this.languageAuto = languageAuto
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user