aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.ts
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-08-15 23:56:49 +0700
committerShulhan <ms@kilabit.info>2021-08-15 23:56:49 +0700
commit4912019d4e3df64fde43bcfd6a8f0519e15c8af3 (patch)
tree6d7992f412831551f46fd18862f6093947602729 /editor/editor.ts
parent19d01aa025cf672f8ea5dd18eda7bf3f2466318f (diff)
downloadpakakeh.ts-4912019d4e3df64fde43bcfd6a8f0519e15c8af3.tar.xz
editor: add method to get file and selection range
The GetFile method return the current file as IVfsNode, including its content (concatenated). The GetSelectionRange return the current selection on the editor.
Diffstat (limited to 'editor/editor.ts')
-rw-r--r--editor/editor.ts68
1 files changed, 45 insertions, 23 deletions
diff --git a/editor/editor.ts b/editor/editor.ts
index ca7c9ac..9e1e301 100644
--- a/editor/editor.ts
+++ b/editor/editor.ts
@@ -48,6 +48,34 @@ export class Editor {
this.range = document.createRange()
}
+ GetFile(): IVfsNode {
+ let node: IVfsNode = {
+ name: "",
+ path: "",
+ }
+ if (!this.activeFile) {
+ return node
+ }
+ let content = ""
+ for (let x = 0; x < this.lines.length; x++) {
+ if (x > 0) {
+ content += "\n"
+ }
+ content += this.lines[x].elText.innerText
+ }
+ node.name = this.activeFile.name
+ node.path = this.activeFile.path
+ node.content = content
+ return node
+ }
+
+ GetSelectionRange(): SelectionRange {
+ return {
+ BeginAt: this.rangeBegin,
+ EndAt: this.rangeEnd,
+ } as SelectionRange
+ }
+
async OpenFile(path: string): Promise<Response> {
if (!this.el) {
return { code: 500 } as Response
@@ -90,9 +118,14 @@ export class Editor {
if (!this.el) {
return
}
- for (let x = 0; x < this.el.children.length; x++) {
+ if (this.rangeEnd == 0) {
+ return
+ }
+ for (let x = this.rangeBegin; x <= this.rangeEnd; x++) {
this.el.children[x].setAttribute("style", "")
}
+ this.rangeBegin = 0
+ this.rangeEnd = 0
}
initStyle() {
@@ -283,8 +316,7 @@ export class Editor {
off = this.sel.focusOffset
if (off > 0) {
textAfter =
- textBefore.slice(0, off - 1) +
- textBefore.slice(off, textBefore.length)
+ textBefore.slice(0, off - 1) + textBefore.slice(off, textBefore.length)
this.unre.DoUpdate(x, textBefore, textAfter)
@@ -297,15 +329,10 @@ export class Editor {
// Join current line with previous.
let elTextPrev = this.lines[x - 1].elText
- this.unre.DoJoin(
- x - 1,
- elTextPrev.innerText,
- elTextCurr.innerText
- )
+ this.unre.DoJoin(x - 1, elTextPrev.innerText, elTextCurr.innerText)
off = elTextPrev.innerText.length
- elTextPrev.innerText =
- elTextPrev.innerText + elTextCurr.innerText
+ elTextPrev.innerText = elTextPrev.innerText + elTextCurr.innerText
this.rawLines[x - 1] = elTextPrev.innerText
// Remove the current line
@@ -325,10 +352,7 @@ export class Editor {
elText = this.lines[x].elText
off = this.sel.focusOffset
textBefore = elText.innerText
- textAfter =
- textBefore.slice(0, off) +
- "\t" +
- textBefore.slice(off, textBefore.length)
+ textAfter = textBefore.slice(0, off) + "\t" + textBefore.slice(off, textBefore.length)
this.unre.DoUpdate(x, textBefore, textAfter)
elText.innerText = textAfter
@@ -360,11 +384,7 @@ export class Editor {
if (this.isKeyControl) {
break
}
- this.unre.DoUpdate(
- x,
- this.rawLines[x],
- this.lines[x].elText.innerText
- )
+ this.unre.DoUpdate(x, this.rawLines[x], this.lines[x].elText.innerText)
this.rawLines[x] = this.lines[x].elText.innerText
}
return true
@@ -439,10 +459,7 @@ export class Editor {
this.el.children[y].setAttribute("style", "")
}
for (; y <= this.rangeEnd; y++) {
- this.el.children[y].setAttribute(
- "style",
- "background-color:lightsalmon"
- )
+ this.el.children[y].setAttribute("style", "background-color:lightsalmon")
}
for (; y < this.el.children.length; y++) {
this.el.children[y].setAttribute("style", "")
@@ -650,3 +667,8 @@ interface ActionChanges {
nextLine: number
nextText: string
}
+
+interface SelectionRange {
+ BeginAt: number
+ EndAt: number
+}