1 import {MarkdownEditorInput, MarkdownEditorInputSelection} from "./interface";
2 import {MarkdownEditorShortcutMap} from "../shortcuts";
3 import {MarkdownEditorEventMap} from "../dom-handlers";
6 export class TextareaInput implements MarkdownEditorInput {
8 protected input: HTMLTextAreaElement;
9 protected shortcuts: MarkdownEditorShortcutMap;
10 protected events: MarkdownEditorEventMap;
12 constructor(input: HTMLTextAreaElement, shortcuts: MarkdownEditorShortcutMap, events: MarkdownEditorEventMap) {
14 this.shortcuts = shortcuts;
17 this.onKeyDown = this.onKeyDown.bind(this);
18 this.configureListeners();
21 configureListeners(): void {
22 // TODO - Teardown handling
23 this.input.addEventListener('keydown', this.onKeyDown);
25 for (const [name, listener] of Object.entries(this.events)) {
26 this.input.addEventListener(name, listener);
30 onKeyDown(e: KeyboardEvent) {
31 const isApple = navigator.platform.startsWith("Mac") || navigator.platform === "iPhone";
33 e.shiftKey ? 'Shift' : null,
34 isApple && e.metaKey ? 'Mod' : null,
35 !isApple && e.ctrlKey ? 'Mod' : null,
39 const keyString = keyParts.filter(Boolean).join('-');
40 if (this.shortcuts[keyString]) {
42 this.shortcuts[keyString]();
46 appendText(text: string): void {
47 this.input.value += `\n${text}`;
50 coordsToSelection(x: number, y: number): MarkdownEditorInputSelection {
52 return this.getSelection();
59 getLineRangeFromPosition(position: number): MarkdownEditorInputSelection {
60 const lines = this.getText().split('\n');
62 for (let i = 0; i < lines.length; i++) {
63 const line = lines[i];
64 const newEnd = lineStart + line.length + 1;
65 if (position < newEnd) {
66 return {from: lineStart, to: newEnd};
71 return {from: 0, to: 0};
74 getLineText(lineIndex: number): string {
75 const text = this.getText();
76 const lines = text.split("\n");
77 return lines[lineIndex] || '';
80 getSelection(): MarkdownEditorInputSelection {
81 return {from: this.input.selectionStart, to: this.input.selectionEnd};
84 getSelectionText(selection?: MarkdownEditorInputSelection): string {
85 const text = this.getText();
86 const range = selection || this.getSelection();
87 return text.slice(range.from, range.to);
91 return this.input.value;
94 getTextAboveView(): string {
95 const scrollTop = this.input.scrollTop;
96 const computedStyles = window.getComputedStyle(this.input);
97 const lines = this.getText().split('\n');
98 const paddingTop = Number(computedStyles.paddingTop.replace('px', ''));
99 const paddingBottom = Number(computedStyles.paddingBottom.replace('px', ''));
101 const avgLineHeight = (this.input.scrollHeight - paddingBottom - paddingTop) / lines.length;
102 const roughLinePos = Math.max(Math.floor((scrollTop - paddingTop) / avgLineHeight), 0);
103 const linesAbove = this.getText().split('\n').slice(0, roughLinePos);
104 return linesAbove.join('\n');
107 searchForLineContaining(text: string): MarkdownEditorInputSelection | null {
108 const textPosition = this.getText().indexOf(text);
109 if (textPosition > -1) {
110 return this.getLineRangeFromPosition(textPosition);
116 setSelection(selection: MarkdownEditorInputSelection, scrollIntoView: boolean): void {
117 this.input.selectionStart = selection.from;
118 this.input.selectionEnd = selection.to;
121 setText(text: string, selection?: MarkdownEditorInputSelection): void {
122 this.input.value = text;
124 this.setSelection(selection, false);
128 spliceText(from: number, to: number, newText: string, selection: Partial<MarkdownEditorInputSelection> | null): void {
129 const text = this.getText();
130 const updatedText = text.slice(0, from) + newText + text.slice(to);
131 this.setText(updatedText);
132 if (selection && selection.from) {
133 const newSelection = {from: selection.from, to: selection.to || selection.from};
134 this.setSelection(newSelection, false);