aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editor/editor.ts')
-rw-r--r--editor/editor.ts45
1 files changed, 22 insertions, 23 deletions
diff --git a/editor/editor.ts b/editor/editor.ts
index 4076474..ca7c9ac 100644
--- a/editor/editor.ts
+++ b/editor/editor.ts
@@ -10,9 +10,12 @@ export interface IEditor {
OpenFile(path: string): Response
SaveFile(node: IVfsNode): Response
+
+ // Handler that will be called when user select lines.
+ OnSelection(begin: number, end: number): void
}
-export class Editor implements IEditor {
+export class Editor {
id: string
is_editable: boolean
private el: HTMLElement | null
@@ -45,15 +48,15 @@ export class Editor implements IEditor {
this.range = document.createRange()
}
- OpenFile(path: string): Response {
- let res: Response = {
- code: 500,
- }
+ async OpenFile(path: string): Promise<Response> {
if (!this.el) {
- return res
+ return { code: 500 } as Response
}
- res = this.opts.OpenFile(path)
+ let res = await this.opts.OpenFile(path)
+ if (!res) {
+ return { code: 500 } as Response
+ }
if (res.code != 200) {
return res
}
@@ -67,6 +70,7 @@ export class Editor implements IEditor {
content = content.replace("\r\n", "\n")
this.rawLines = content.split("\n")
+ this.lines = []
for (let x = 0; x < this.rawLines.length; x++) {
let line = new EditorLine(x, this.rawLines[x], this)
this.lines.push(line)
@@ -102,6 +106,7 @@ export class Editor implements IEditor {
background-color: cornsilk;
font-family: monospace;
width: 100%;
+ overflow-y: scroll;
}
.wui-editor-line {
display: table;
@@ -279,16 +284,9 @@ export class Editor implements IEditor {
if (off > 0) {
textAfter =
textBefore.slice(0, off - 1) +
- textBefore.slice(
- off,
- textBefore.length,
- )
+ textBefore.slice(off, textBefore.length)
- this.unre.DoUpdate(
- x,
- textBefore,
- textAfter,
- )
+ this.unre.DoUpdate(x, textBefore, textAfter)
elTextCurr.innerText = textAfter
this.rawLines[x] = textAfter
@@ -302,13 +300,12 @@ export class Editor implements IEditor {
this.unre.DoJoin(
x - 1,
elTextPrev.innerText,
- elTextCurr.innerText,
+ elTextCurr.innerText
)
off = elTextPrev.innerText.length
elTextPrev.innerText =
- elTextPrev.innerText +
- elTextCurr.innerText
+ elTextPrev.innerText + elTextCurr.innerText
this.rawLines[x - 1] = elTextPrev.innerText
// Remove the current line
@@ -366,10 +363,9 @@ export class Editor implements IEditor {
this.unre.DoUpdate(
x,
this.rawLines[x],
- this.lines[x].elText.innerText,
- )
- this.rawLines[x] =
this.lines[x].elText.innerText
+ )
+ this.rawLines[x] = this.lines[x].elText.innerText
}
return true
}
@@ -445,12 +441,15 @@ export class Editor implements IEditor {
for (; y <= this.rangeEnd; y++) {
this.el.children[y].setAttribute(
"style",
- "background-color:lightsalmon",
+ "background-color:lightsalmon"
)
}
for (; y < this.el.children.length; y++) {
this.el.children[y].setAttribute("style", "")
}
+ if (this.opts.OnSelection) {
+ this.opts.OnSelection(this.rangeBegin, this.rangeEnd)
+ }
}
render() {