-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track editor state changes in more clear way
- Loading branch information
Showing
8 changed files
with
152 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { watch, type Ref } from 'vue'; | ||
import type { Editor } from 'brace'; | ||
|
||
import onRefAssigned from './onRefAssigned'; | ||
|
||
export default function bindEditorScroll(editor: Ref<Editor | undefined>, scroll: Ref<number | undefined>): void { | ||
onRefAssigned(editor, (value) => { | ||
const session = value.getSession(); | ||
session.on('changeScrollTop', () => { | ||
const scrollTop = session.getScrollTop(); | ||
if (scroll.value != scrollTop) { | ||
scroll.value = scrollTop; | ||
} | ||
}); | ||
}); | ||
|
||
watch(scroll, (value) => { | ||
if (typeof value != 'undefined' && editor.value) { | ||
const session = editor.value.getSession(); | ||
if (session.getScrollTop() !== value) { | ||
session.setScrollTop(value); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { watch, type Ref } from 'vue'; | ||
import type { Editor, Range, VirtualRenderer } from 'brace'; | ||
|
||
import onRefAssigned from './onRefAssigned'; | ||
|
||
import ISelection from '@/types/selection'; | ||
|
||
interface Renderer extends VirtualRenderer { | ||
scrollTop: number; | ||
scrollSelectionIntoView(): void; | ||
} | ||
|
||
export default function bindEditorSelection(editorRef: Ref<Editor | undefined>, selection: Ref<ISelection | undefined>): void { | ||
onRefAssigned(editorRef, (editor) => { | ||
applyRange(editor, toRange({ row: 0, offset: 0, length: 0 })); | ||
|
||
editor.selection.on('changeCursor', () => { | ||
const range = editor.getSelection().getRange(); | ||
selection.value = toSelection(range, 'editor'); | ||
}); | ||
}); | ||
|
||
watch(selection, (selection) => { | ||
const editor = editorRef.value; | ||
// Do not respond to selection changes dispatched from the editor | ||
// This can't be done with a comparation between two selection objects, | ||
// since the object generated from the editor is different, especially on | ||
// multi-line selections or deletions. | ||
if (editor && selection && selection.from != 'editor') { | ||
applyRange(editor, toRange(selection)); | ||
} | ||
}); | ||
} | ||
|
||
function toRange({ row, offset, length }: Omit<ISelection, 'from'>): Range { | ||
return { | ||
start: { row: row, column: offset }, | ||
end: { row: row, column: offset + length }, | ||
} as Range; | ||
} | ||
|
||
function applyRange(editor: Editor, range: Range) { | ||
const renderer = editor.renderer as Renderer; | ||
editor.selection.setRange(range, false); | ||
renderer.scrollToX(0); | ||
renderer.scrollSelectionIntoView(); | ||
editor.focus(); | ||
} | ||
|
||
function toSelection(range: Range, from: ISelection['from']): ISelection { | ||
return { | ||
row: range.start.row, | ||
offset: range.start.column, | ||
length: range.end.column - range.start.column, | ||
from: from, | ||
} as ISelection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Ref } from 'vue'; | ||
import type { Editor } from 'brace'; | ||
|
||
import onRefAssigned from './onRefAssigned'; | ||
|
||
export default function bindEditorValue(editor: Ref<Editor | undefined>, val: Ref<string | undefined>): void { | ||
onRefAssigned(editor, (value) => { | ||
if (val.value) { | ||
value.setValue(val.value); | ||
} | ||
|
||
value.on('change', () => { | ||
val.value = value.getValue(); | ||
}); | ||
}); | ||
|
||
// only one way binding | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type WatchSource, type WatchStopHandle, watch } from 'vue'; | ||
|
||
export default function onRefAssigned<T>(value: WatchSource<T | undefined>, callback: (newValue: T) => void): WatchStopHandle { | ||
return watch(value, (newValue, oldValue) => { | ||
if (typeof newValue != 'undefined' && typeof oldValue == 'undefined') { | ||
callback(newValue); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type WatchSource, type WatchStopHandle, watch } from 'vue'; | ||
|
||
export default function onRefUnassigned<T>(value: WatchSource<T | undefined>, callback: (oldValue: T) => void): WatchStopHandle { | ||
return watch(value, (newValue, oldValue) => { | ||
if (typeof newValue == 'undefined' && typeof oldValue != 'undefined') { | ||
callback(oldValue); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters