basic button affecting editor
This commit is contained in:
parent
d7d34b2326
commit
00ff13ae08
@ -1,6 +1,8 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<h1>EDITOR</h1>
|
<h1>EDITOR</h1>
|
||||||
|
|
||||||
|
<b-button @click="toolbarTest()">Test</b-button>
|
||||||
<div id="editor" style="background-color: #fff; border: solid 1px">
|
<div id="editor" style="background-color: #fff; border: solid 1px">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -12,7 +14,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
import {EditorState} from "@codemirror/state"
|
import {EditorSelection, EditorState} from "@codemirror/state"
|
||||||
import {keymap, EditorView, MatchDecorator, Decoration, WidgetType, ViewPlugin, DecorationSet} from "@codemirror/view"
|
import {keymap, EditorView, MatchDecorator, Decoration, WidgetType, ViewPlugin, DecorationSet} from "@codemirror/view"
|
||||||
import {defaultKeymap, history} from "@codemirror/commands"
|
import {defaultKeymap, history} from "@codemirror/commands"
|
||||||
|
|
||||||
@ -78,6 +80,7 @@ export default {
|
|||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
editor_view: null,
|
||||||
ingredients: [
|
ingredients: [
|
||||||
{amount: 20, food: {'name': 'raspberry'}, unit: {'name': 'pcs'}},
|
{amount: 20, food: {'name': 'raspberry'}, unit: {'name': 'pcs'}},
|
||||||
{amount: 100, food: {'name': 'sugar'}, unit: {'name': 'g'}},
|
{amount: 100, food: {'name': 'sugar'}, unit: {'name': 'g'}},
|
||||||
@ -123,7 +126,7 @@ export default {
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
let view = new EditorView({
|
this.editor_view = new EditorView({
|
||||||
state: startState,
|
state: startState,
|
||||||
extensions: [],
|
extensions: [],
|
||||||
parent: document.getElementById("editor")
|
parent: document.getElementById("editor")
|
||||||
@ -139,6 +142,41 @@ export default {
|
|||||||
options: this.autocomplete_options
|
options: this.autocomplete_options
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
toolbarTest() {
|
||||||
|
const transaction = this.editor_view.state.changeByRange((range) => {
|
||||||
|
const prefix = '###'
|
||||||
|
const docText = this.editor_view.state.doc.toString()
|
||||||
|
let text = this.editor_view.state.sliceDoc(range.from, range.to)
|
||||||
|
|
||||||
|
let rangeFrom = range.from
|
||||||
|
|
||||||
|
while (rangeFrom > 0) {
|
||||||
|
if (docText[rangeFrom - 1] === "\n") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
rangeFrom -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
text = this.editor_view.state.sliceDoc(rangeFrom, range.to)
|
||||||
|
const textBefore = `\n${text}`
|
||||||
|
const newText = textBefore.replace(/\n/g, `\n${prefix}`)
|
||||||
|
|
||||||
|
const changes = {
|
||||||
|
from: rangeFrom,
|
||||||
|
to: range.to,
|
||||||
|
insert: newText,
|
||||||
|
anchor: rangeFrom + prefix.length + 1,
|
||||||
|
textBefore: textBefore
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
changes,
|
||||||
|
range: EditorSelection.range(changes.anchor, changes.anchor)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
this.editor_view.dispatch(transaction)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user