diff --git a/editmode.ts b/editmode.ts index aa377bd..904fb1a 100644 --- a/editmode.ts +++ b/editmode.ts @@ -46,9 +46,33 @@ const codeTabState = { this.code = code.slice(0, Math.min(anchor, focus)) + text + code.slice(Math.max(anchor, focus)); this.setSelection(Math.min(anchor, focus) + text.length); }, - // indent(char) { - // const lines = this.code.split("\n"). - // }, + indent(indentString: string) { + const lines = this.code.split("\n"); + const {focusY, anchorY} = this; + const newLines = lines.map((line, i) => { + console.log(i, Math.min(focusY, anchorY), Math.max(focusY, anchorY)); + if (i >= Math.min(focusY, anchorY) && i <= Math.max(focusY, anchorY)) { + console.log(indentString+line); + return indentString+line; + } else { + return line; + } + }); + this.code = newLines.join("\n"); + }, + outdent(outdentRegex: RegExp) { + const lines = this.code.split("\n"); + const {focusY, anchorY} = this; + const newLines = lines.map((line, i) => { + const match = line.match(outdentRegex); + if (i >= Math.min(focusY, anchorY) && i <= Math.max(focusY, anchorY) && match) { + return line.slice(match[0].length); + } else { + return line; + } + }); + this.code = newLines.join("\n"); + }, backspace() { const {code, focus} = this; if (this.isCollapsed()) { @@ -127,12 +151,12 @@ const update = () => { if (keyPressed(K.TAB)) { if (!shiftKeyDown()) { if (codeTabState.isCollapsed()) { - codeTabState.insertText("\n"); + codeTabState.insertText("\t"); } else { - // codeTabState.indent("\t"); + codeTabState.indent("\t"); } } else { - // codeTabState.outdent(/\t| /); + codeTabState.outdent(/^(\t| )/); } } if (keyPressed(K.BACKSPACE)) {