diff --git a/src/keybindings/vim.ts b/src/keybindings/vim.ts index 75c5c82..6b86007 100644 --- a/src/keybindings/vim.ts +++ b/src/keybindings/vim.ts @@ -92,6 +92,6 @@ onConfigurationChanged(() => { } }) -export function initVimMode (editor: monaco.editor.IStandaloneCodeEditor, statusBarElement: Element): IDisposable { +export function initVimMode (editor: monaco.editor.ICodeEditor, statusBarElement: Element): IDisposable { return monacoVim.initVimMode(editor, statusBarElement) } diff --git a/src/tools.ts b/src/tools.ts index 92a464f..897e575 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -1,4 +1,5 @@ import * as monaco from 'monaco-editor' +import { ContextKeyExpr, DisposableStore, KeybindingsRegistry } from 'vscode/monaco' interface PastePayload { text: string @@ -12,11 +13,12 @@ function isPasteAction (handlerId: string, payload: unknown): payload is PastePa } export function lockCodeWithoutDecoration ( - editor: monaco.editor.IStandaloneCodeEditor, - decorations: string[], + editor: monaco.editor.ICodeEditor, + decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean, allowChangeFromSources: string[] = [], errorMessage?: string -): () => void { +): monaco.IDisposable { + const disposableStore = new DisposableStore() function displayLockedCodeError (position: monaco.Position) { if (errorMessage == null) { return @@ -29,8 +31,14 @@ export function lockCodeWithoutDecoration ( function canEditRange (range: monaco.IRange) { const model = editor.getModel() if (model != null) { - const editableRanges = decorations.map(decoration => model.getDecorationRange(decoration)) - return editableRanges.some(editableRange => editableRange?.containsRange(range) ?? false) + const editableRanges = model + .getAllDecorations() + .filter(decorationFilter) + .map((decoration) => decoration.range) + if (editableRanges.length === 0) { + return true + } + return editableRanges.some((editableRange) => editableRange.containsRange(range)) } return false } @@ -51,7 +59,13 @@ export function lockCodeWithoutDecoration ( const originalTrigger = editor.trigger editor.trigger = function (source, handlerId, payload) { // Try to transform whole file pasting into a paste in the editable area only - const lastEditableRange = decorations.length > 0 ? editor.getModel()?.getDecorationRange(decorations[decorations.length - 1]!) : null + const editableRanges = editor + .getModel()! + .getAllDecorations() + .filter(decorationFilter) + .map((decoration) => decoration.range) + const lastEditableRange = + editableRanges.length > 0 ? editableRanges[editableRanges.length - 1] : undefined if (isPasteAction(handlerId, payload) && lastEditableRange != null) { const selections = editor.getSelections() const model = editor.getModel()! @@ -62,10 +76,12 @@ export function lockCodeWithoutDecoration ( if (wholeFileSelected) { const currentEditorValue = editor.getValue() const before = model.getOffsetAt(lastEditableRange.getStartPosition()) - const after = currentEditorValue.length - model.getOffsetAt(lastEditableRange.getEndPosition()) + const after = + currentEditorValue.length - model.getOffsetAt(lastEditableRange.getEndPosition()) if ( currentEditorValue.slice(0, before) === payload.text.slice(0, before) && - currentEditorValue.slice(currentEditorValue.length - after) === payload.text.slice(payload.text.length - after) + currentEditorValue.slice(currentEditorValue.length - after) === + payload.text.slice(payload.text.length - after) ) { editor.setSelection(lastEditableRange) const newPayload: PastePayload = { @@ -80,7 +96,7 @@ export function lockCodeWithoutDecoration ( if (['type', 'paste', 'cut'].includes(handlerId)) { const selections = editor.getSelections() - if (selections != null && selections.some(range => !canEditRange(range))) { + if (selections != null && selections.some((range) => !canEditRange(range))) { displayLockedCodeError(editor.getPosition()!) return } @@ -106,16 +122,23 @@ export function lockCodeWithoutDecoration ( if (model == null) { return } - const originalApplyEdit: (operations: monaco.editor.IIdentifiedSingleEditOperation[], computeUndoEdits?: boolean) => void = model.applyEdits - model.applyEdits = ((operations: monaco.editor.IIdentifiedSingleEditOperation[], computeUndoEdits?: boolean) => { + const originalApplyEdit: ( + operations: monaco.editor.IIdentifiedSingleEditOperation[], + computeUndoEdits?: boolean + ) => void = model.applyEdits + model.applyEdits = (( + operations: monaco.editor.IIdentifiedSingleEditOperation[], + computeUndoEdits?: boolean + ) => { if (currentEditSource != null && allowChangeFromSources.includes(currentEditSource)) { return originalApplyEdit.call(model, operations, computeUndoEdits!) } - const filteredOperations = operations - .filter(operation => canEditRange(operation.range)) + const filteredOperations = operations.filter((operation) => canEditRange(operation.range)) if (filteredOperations.length === 0 && operations.length > 0) { const firstRange = operations[0]!.range - displayLockedCodeError(new monaco.Position(firstRange.startLineNumber, firstRange.startColumn)) + displayLockedCodeError( + new monaco.Position(firstRange.startLineNumber, firstRange.startColumn) + ) } return originalApplyEdit.call(model, filteredOperations, computeUndoEdits!) }) as typeof model.applyEdits @@ -124,45 +147,57 @@ export function lockCodeWithoutDecoration ( model.applyEdits = originalApplyEdit as typeof model.applyEdits } } - const editorChangeModelDisposable = editor.onDidChangeModel(lockModel) + disposableStore.add(editor.onDidChangeModel(lockModel)) lockModel() // Handle selection of the last line of an editable range - const selectionDisposable = editor.onDidChangeCursorSelection(e => { - if (canEditRange(e.selection)) { - return - } - const model = editor.getModel() - if (model == null) { - return - } - const shiftedRange = monaco.Range.fromPositions( - model.getPositionAt(model.getOffsetAt(e.selection.getStartPosition()) - 1), - model.getPositionAt(model.getOffsetAt(e.selection.getEndPosition()) - 1) - ) - if (canEditRange(shiftedRange)) { - editor.setSelection(shiftedRange) + disposableStore.add( + editor.onDidChangeCursorSelection((e) => { + if (canEditRange(e.selection)) { + return + } + const model = editor.getModel() + if (model == null) { + return + } + const shiftedRange = monaco.Range.fromPositions( + model.getPositionAt(model.getOffsetAt(e.selection.getStartPosition()) - 1), + model.getPositionAt(model.getOffsetAt(e.selection.getEndPosition()) - 1) + ) + if (canEditRange(shiftedRange)) { + editor.setSelection(shiftedRange) + } + }) + ) + + disposableStore.add({ + dispose () { + restoreModelApplyEdit() + editor.executeEdits = originalExecuteEdit + editor.executeCommands = originalExecuteCommands + editor.trigger = originalTrigger } }) - return () => { - selectionDisposable.dispose() - restoreModelApplyEdit() - editorChangeModelDisposable.dispose() - editor.executeEdits = originalExecuteEdit - editor.executeCommands = originalExecuteCommands - editor.trigger = originalTrigger - } + return disposableStore } -export function hideCodeWithoutDecoration (editor: monaco.editor.IStandaloneCodeEditor, decorations: string[]): () => void { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let otherHiddenAreas: monaco.IRange[] = (editor as any)._getViewModel()._lines.getHiddenAreas() +export function hideCodeWithoutDecoration (editor: monaco.editor.ICodeEditor, decorationFilter: (decoration: monaco.editor.IModelDecoration) => boolean): monaco.IDisposable { + let otherHiddenAreas: monaco.IRange[] = editor._getViewModel()?.getHiddenAreas() ?? [] function getHiddenAreas () { - const model = editor.getModel()! + const model = editor.getModel() + if (model == null) { + return [] + } + + const decorations = model.getAllDecorations() + .filter(decorationFilter) + if (decorations.length === 0) { + return otherHiddenAreas + } const ranges = decorations - .map(decoration => model.getDecorationRange(decoration)!) + .map(decoration => decoration.range) .sort((a, b) => a.startLineNumber - b.startLineNumber) // merge ranges .reduce((acc, range) => { @@ -213,18 +248,31 @@ export function hideCodeWithoutDecoration (editor: monaco.editor.IStandaloneCode updateHiddenAreas() } + const disposableStore = new DisposableStore() + + disposableStore.add(editor.onDidChangeModel(() => { + otherHiddenAreas = editor._getViewModel()?.getHiddenAreas() ?? [] + updateHiddenAreas() + })) + disposableStore.add(editor.onDidChangeModelDecorations(() => { + updateHiddenAreas() + })) updateHiddenAreas() - return () => { - editor.setHiddenAreas = originalSetHiddenAreas - editor.setHiddenAreas(otherHiddenAreas) - } + disposableStore.add({ + dispose () { + editor.setHiddenAreas = originalSetHiddenAreas + editor.setHiddenAreas(otherHiddenAreas) + } + }) + + return disposableStore } /** * Collapse everything between startToken and endToken */ -export async function collapseCodeSections (editor: monaco.editor.IStandaloneCodeEditor, startToken: string, endToken: string, isRegex: boolean = false): Promise { +export async function collapseCodeSections (editor: monaco.editor.ICodeEditor, startToken: string, endToken: string, isRegex: boolean = false): Promise { const editorModel = editor.getModel() const ranges: monaco.IRange[] = [] if (editorModel != null) { @@ -269,3 +317,277 @@ export async function collapseCodeSections (editor: monaco.editor.IStandaloneCod } } } + +interface IDecorationProvider { + provideDecorations (model: monaco.editor.ITextModel): monaco.editor.IModelDeltaDecoration[] +} + +export function registerTextDecorationProvider (provider: IDecorationProvider): monaco.IDisposable { + const disposableStore = new DisposableStore() + + const watchEditor = (editor: monaco.editor.ICodeEditor): monaco.IDisposable => { + const disposableStore = new DisposableStore() + const decorationCollection = editor.createDecorationsCollection() + + const checkEditor = () => { + const model = editor.getModel() + if (model != null) { + decorationCollection.set(provider.provideDecorations(model)) + } else { + decorationCollection.clear() + } + } + + disposableStore.add(editor.onDidChangeModel(checkEditor)) + disposableStore.add(editor.onDidChangeModelContent(checkEditor)) + disposableStore.add({ + dispose () { + decorationCollection.clear() + } + }) + checkEditor() + return disposableStore + } + + monaco.editor.getEditors().forEach(editor => disposableStore.add(watchEditor(editor))) + disposableStore.add(monaco.editor.onDidCreateEditor(editor => disposableStore.add(watchEditor(editor)))) + + return disposableStore +} + +export function runOnAllEditors (cb: (editor: monaco.editor.ICodeEditor) => monaco.IDisposable): monaco.IDisposable { + const disposableStore = new DisposableStore() + + const handleEditor = (editor: monaco.editor.ICodeEditor) => { + const disposable = cb(editor) + disposableStore.add(disposable) + const disposeEventDisposable = editor.onDidDispose(() => { + disposableStore.delete(disposable) + disposableStore.delete(disposeEventDisposable) + }) + disposableStore.add(disposeEventDisposable) + } + monaco.editor.getEditors().forEach(handleEditor) + disposableStore.add(monaco.editor.onDidCreateEditor(handleEditor)) + + return disposableStore +} + +export function preventAlwaysConsumeTouchEvent (editor: monaco.editor.ICodeEditor): void { + let firstX = 0 + let firstY = 0 + let atTop = false + let atBottom = false + let atLeft = false + let atRight = false + let useBrowserBehavior: null | boolean = null + + editor.onDidChangeModel(() => { + const domNode = editor.getDomNode() + if (domNode == null) { + return + } + domNode.addEventListener('touchstart', (e) => { + const firstTouch = e.targetTouches.item(0) + if (firstTouch == null) { + return + } + + // Prevent monaco-editor from trying to call preventDefault on the touchstart event + // so we'll be able to use the default behavior of the touchmove event + e.preventDefault = () => {} + + firstX = firstTouch.clientX + firstY = firstTouch.clientY + + const layoutInfo = editor.getLayoutInfo() + atTop = editor.getScrollTop() <= 0 + atBottom = editor.getScrollTop() >= editor.getContentHeight() - layoutInfo.height + atLeft = editor.getScrollLeft() <= 0 + atRight = editor.getScrollLeft() >= editor.getContentWidth() - layoutInfo.width + useBrowserBehavior = null + }) + domNode.addEventListener('touchmove', (e) => { + const firstTouch = e.changedTouches.item(0) + if (firstTouch == null) { + return + } + + if (useBrowserBehavior == null) { + const dx = firstTouch.clientX - firstX + const dy = firstTouch.clientY - firstY + if (Math.abs(dx) > Math.abs(dy)) { + // It's an horizontal scroll + useBrowserBehavior = (dx < 0 && atRight) || (dx > 0 && atLeft) + } else { + // It's a vertical scroll + useBrowserBehavior = (dy < 0 && atBottom) || (dy > 0 && atTop) + } + } + if (useBrowserBehavior) { + // Stop the event before monaco tries to preventDefault on it + e.stopPropagation() + } + }) + domNode.addEventListener('touchend', (e) => { + if (useBrowserBehavior ?? false) { + // Prevent monaco from trying to open its context menu + // It thinks it's a long press because it didn't receive the move events + e.stopPropagation() + } + }) + }) +} + +// https://github.com/microsoft/monaco-editor/issues/568 +class PlaceholderContentWidget implements monaco.editor.IContentWidget { + private static readonly ID = 'editor.widget.placeholderHint' + + private domNode: HTMLElement | undefined + + constructor ( + private readonly editor: monaco.editor.ICodeEditor, + private readonly placeholder: string + ) {} + + getId (): string { + return PlaceholderContentWidget.ID + } + + getDomNode (): HTMLElement { + if (this.domNode == null) { + this.domNode = document.createElement('pre') + this.domNode.style.width = 'max-content' + this.domNode.textContent = this.placeholder + this.domNode.style.pointerEvents = 'none' + this.domNode.style.color = '#aaa' + this.domNode.style.margin = '0' + + this.editor.applyFontInfo(this.domNode) + } + + return this.domNode + } + + getPosition (): monaco.editor.IContentWidgetPosition | null { + return { + position: { lineNumber: 1, column: 1 }, + preference: [monaco.editor.ContentWidgetPositionPreference.EXACT] + } + } +} + +export function addPlaceholder ( + editor: monaco.editor.ICodeEditor, + placeholder: string +): monaco.IDisposable { + const widget = new PlaceholderContentWidget(editor, placeholder) + + function onDidChangeModelContent (): void { + if (editor.getValue() === '') { + editor.addContentWidget(widget) + } else { + editor.removeContentWidget(widget) + } + } + + onDidChangeModelContent() + const changeDisposable = editor.onDidChangeModelContent(() => onDidChangeModelContent()) + return { + dispose () { + changeDisposable.dispose() + editor.removeContentWidget(widget) + } + } +} + +export function mapClipboard ( + editor: monaco.editor.ICodeEditor, + { + toClipboard, + fromClipboard + }: { + toClipboard: (data: string) => string + fromClipboard: (data: string) => string + } +): monaco.IDisposable { + const disposableStore = new DisposableStore() + let copiedText = '' + + disposableStore.add( + KeybindingsRegistry.registerCommandAndKeybindingRule({ + id: 'customCopy', + weight: 1000, + handler: () => { + copiedText = editor.getModel()!.getValueInRange(editor.getSelection()!) + document.execCommand('copy') + }, + when: ContextKeyExpr.equals('editorId', editor.getId()), + primary: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyC + }) + ) + + disposableStore.add( + KeybindingsRegistry.registerCommandAndKeybindingRule({ + id: 'customCut', + weight: 1000, + handler: () => { + copiedText = editor.getModel()!.getValueInRange(editor.getSelection()!) + document.execCommand('copy') + }, + when: ContextKeyExpr.equals('editorId', editor.getId()), + primary: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyX + }) + ) + + const originalTrigger = editor.trigger + editor.trigger = function (source, handlerId, payload) { + if (handlerId === 'editor.action.clipboardCopyAction') { + copiedText = editor.getModel()!.getValueInRange(editor.getSelection()!) + } else if (handlerId === 'editor.action.clipboardCutAction') { + copiedText = editor.getModel()!.getValueInRange(editor.getSelection()!) + } else if (handlerId === 'paste') { + const newText = fromClipboard(payload.text) + if (newText !== payload.text) { + payload = { + ...payload, + text: newText + } + } + } + originalTrigger.call(this, source, handlerId, payload) + } + disposableStore.add({ + dispose () { + editor.trigger = originalTrigger + } + }) + + function mapCopy (event: ClipboardEvent): void { + const clipdata = event.clipboardData ?? (window as unknown as { clipboardData: DataTransfer }).clipboardData + let content = clipdata.getData('Text') + if (content.length === 0) { + content = copiedText + } + const transformed = toClipboard(content) + if (transformed !== content) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (clipdata.types != null) { + clipdata.types.forEach(type => clipdata.setData(type, toClipboard(content))) + } else { + clipdata.setData('text/plain', toClipboard(content)) + } + } + } + const editorDomNode = editor.getContainerDomNode() + editorDomNode.addEventListener('copy', mapCopy) + editorDomNode.addEventListener('cut', mapCopy) + disposableStore.add({ + dispose () { + editorDomNode.removeEventListener('copy', mapCopy) + editorDomNode.removeEventListener('cut', mapCopy) + } + }) + + return disposableStore +} diff --git a/src/tools/EditorOpenHandlerRegistry.ts b/src/tools/EditorOpenHandlerRegistry.ts index c3dba8f..55796a1 100644 --- a/src/tools/EditorOpenHandlerRegistry.ts +++ b/src/tools/EditorOpenHandlerRegistry.ts @@ -5,7 +5,7 @@ import { createEditor } from '../monaco' let currentEditor: ({ model: monaco.editor.ITextModel - editor: monaco.editor.IStandaloneCodeEditor + editor: monaco.editor.ICodeEditor } & monaco.IDisposable) | null = null function openNewCodeEditor (modelRef: IReference) { if (currentEditor != null && modelRef.object.textEditorModel === currentEditor.model) { diff --git a/src/types/monaco-vim.d.ts b/src/types/monaco-vim.d.ts index 636240c..31effee 100644 --- a/src/types/monaco-vim.d.ts +++ b/src/types/monaco-vim.d.ts @@ -1,7 +1,7 @@ declare module 'monaco-vim' { import * as monaco from 'monaco-editor' export const initVimMode: ( - editor: monaco.editor.IStandaloneCodeEditor, + editor: monaco.editor.ICodeEditor, statusBarElement: Element ) => monaco.IDisposable export const VimMode: { diff --git a/src/types/monaco.d.ts b/src/types/monaco.d.ts index 802e09b..2ca02f2 100644 --- a/src/types/monaco.d.ts +++ b/src/types/monaco.d.ts @@ -2,10 +2,14 @@ import { IRange } from 'monaco-editor' declare module 'monaco-editor' { namespace editor { - interface IStandaloneCodeEditor { + interface ICodeEditor { // This method is internal and is supposed to be used by the folding feature // We still use it to hide parts of the code in the `hideCodeWithoutDecoration` function setHiddenAreas(ranges: IRange[]): void + + _getViewModel(): { + getHiddenAreas(): IRange[] + } | undefined } } }