未验证 提交 b4631d39 编写于 作者: A Alex Dima

Emit ReadOnlyEditAttemptEvent through view model

上级 81e71a4a
......@@ -1473,10 +1473,6 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
// Someone might destroy the model from under the editor, so prevent any exceptions by setting a null model
listenersToRemove.push(model.onWillDispose(() => this.setModel(null)));
listenersToRemove.push(viewModel.cursor.onDidAttemptReadOnlyEdit(() => {
this._onDidAttemptReadOnlyEdit.fire(undefined);
}));
listenersToRemove.push(viewModel.onEvent((e) => {
switch (e.kind) {
case OutgoingViewModelEventKind.ContentSizeChanged:
......@@ -1491,6 +1487,9 @@ export class CodeEditorWidget extends Disposable implements editorBrowser.ICodeE
case OutgoingViewModelEventKind.ViewZonesChanged:
this._onDidChangeViewZones.fire();
break;
case OutgoingViewModelEventKind.ReadOnlyEditAttempt:
this._onDidAttemptReadOnlyEdit.fire();
break;
case OutgoingViewModelEventKind.CursorStateChanged: {
if (e.reachedMaxCursorCount) {
this._notificationService.warn(nls.localize('cursors.maximum', "The number of cursors has been limited to {0}.", Cursor.MAX_CURSOR_COUNT));
......
......@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { onUnexpectedError } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import * as strings from 'vs/base/common/strings';
import { CursorCollection } from 'vs/editor/common/controller/cursorCollection';
import { CursorColumns, CursorConfiguration, CursorContext, CursorState, EditOperationResult, EditOperationType, IColumnSelectData, PartialCursorState, ICursorSimpleModel } from 'vs/editor/common/controller/cursorCommon';
......@@ -124,9 +123,6 @@ export class Cursor extends Disposable {
public static readonly MAX_CURSOR_COUNT = 10000;
private readonly _onDidAttemptReadOnlyEdit: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidAttemptReadOnlyEdit: Event<void> = this._onDidAttemptReadOnlyEdit.event;
private readonly _model: ITextModel;
private _knownModelVersionId: number;
private readonly _viewModel: ICursorSimpleModel;
......@@ -598,7 +594,6 @@ export class Cursor extends Disposable {
private _executeEdit(callback: () => void, eventsCollector: ViewModelEventsCollector, source: string | null | undefined, cursorChangeReason: CursorChangeReason = CursorChangeReason.NotSet): void {
if (this.context.cursorConfig.readOnly) {
// we cannot edit when read only...
this._onDidAttemptReadOnlyEdit.fire(undefined);
return;
}
......@@ -621,15 +616,17 @@ export class Cursor extends Disposable {
}
}
public setIsDoingComposition(isDoingComposition: boolean): void {
this._isDoingComposition = isDoingComposition;
}
public startComposition(eventsCollector: ViewModelEventsCollector): void {
this._isDoingComposition = true;
this._selectionsWhenCompositionStarted = this.getSelections().slice(0);
}
public endComposition(eventsCollector: ViewModelEventsCollector, source?: string | null | undefined): void {
this._isDoingComposition = false;
this._executeEdit(() => {
if (!this._isDoingComposition && source === 'keyboard') {
if (source === 'keyboard') {
// composition finishes, let's check if we need to auto complete if necessary.
const autoClosedCharacters = AutoClosedAction.getAllAutoClosedCharacters(this._autoClosedActions);
this._executeEditOperation(TypeOperations.compositionEndWithInterceptors(this._prevEditOperationType, this.context.cursorConfig, this._model, this._selectionsWhenCompositionStarted, this.getSelections(), autoClosedCharacters));
......
......@@ -176,6 +176,7 @@ export const enum OutgoingViewModelEventKind {
FocusChanged,
ScrollChanged,
ViewZonesChanged,
ReadOnlyEditAttempt,
CursorStateChanged,
}
......@@ -366,10 +367,27 @@ export class CursorStateChangedEvent {
}
}
export class ReadOnlyEditAttemptEvent {
public readonly kind = OutgoingViewModelEventKind.ReadOnlyEditAttempt;
constructor() {
}
public isNoOp(): boolean {
return false;
}
public merge(other: OutgoingViewModelEvent): ReadOnlyEditAttemptEvent {
return this;
}
}
export type OutgoingViewModelEvent = (
ContentSizeChangedEvent
| FocusChangedEvent
| ScrollChangedEvent
| ViewZonesChangedEvent
| ReadOnlyEditAttemptEvent
| CursorStateChangedEvent
);
......@@ -30,7 +30,7 @@ import { Cursor } from 'vs/editor/common/controller/cursor';
import { PartialCursorState, CursorState, IColumnSelectData, EditOperationType, CursorConfiguration } from 'vs/editor/common/controller/cursorCommon';
import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents';
import { IWhitespaceChangeAccessor } from 'vs/editor/common/viewLayout/linesLayout';
import { ViewModelEventDispatcher, OutgoingViewModelEvent, FocusChangedEvent, ScrollChangedEvent, ViewZonesChangedEvent, ViewModelEventsCollector } from 'vs/editor/common/viewModel/viewModelEventDispatcher';
import { ViewModelEventDispatcher, OutgoingViewModelEvent, FocusChangedEvent, ScrollChangedEvent, ViewZonesChangedEvent, ViewModelEventsCollector, ReadOnlyEditAttemptEvent } from 'vs/editor/common/viewModel/viewModelEventDispatcher';
import { ViewEventHandler } from 'vs/editor/common/viewModel/viewEventHandler';
const USE_IDENTITY_LINES_COLLECTION = true;
......@@ -52,7 +52,7 @@ export class ViewModel extends Disposable implements IViewModel {
private readonly lines: IViewModelLinesCollection;
public readonly coordinatesConverter: ICoordinatesConverter;
public readonly viewLayout: ViewLayout;
public readonly cursor: Cursor;
private readonly cursor: Cursor;
private readonly decorations: ViewModelDecorations;
constructor(
......@@ -914,32 +914,42 @@ export class ViewModel extends Disposable implements IViewModel {
this._withViewEventsCollector(eventsCollector => this.cursor.restoreState(eventsCollector, states));
}
private _executeCursorEdit(callback: (eventsCollector: ViewModelEventsCollector) => void): void {
if (this.cursor.context.cursorConfig.readOnly) {
// we cannot edit when read only...
this._eventDispatcher.emitOutgoingEvent(new ReadOnlyEditAttemptEvent());
return;
}
this._withViewEventsCollector(callback);
}
public executeEdits(source: string | null | undefined, edits: IIdentifiedSingleEditOperation[], cursorStateComputer: ICursorStateComputer): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeEdits(eventsCollector, source, edits, cursorStateComputer));
this._executeCursorEdit(eventsCollector => this.cursor.executeEdits(eventsCollector, source, edits, cursorStateComputer));
}
public startComposition(): void {
this._withViewEventsCollector(eventsCollector => this.cursor.startComposition(eventsCollector));
this.cursor.setIsDoingComposition(true);
this._executeCursorEdit(eventsCollector => this.cursor.startComposition(eventsCollector));
}
public endComposition(source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.endComposition(eventsCollector, source));
this.cursor.setIsDoingComposition(false);
this._executeCursorEdit(eventsCollector => this.cursor.endComposition(eventsCollector, source));
}
public type(text: string, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.type(eventsCollector, text, source));
this._executeCursorEdit(eventsCollector => this.cursor.type(eventsCollector, text, source));
}
public replacePreviousChar(text: string, replaceCharCnt: number, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.replacePreviousChar(eventsCollector, text, replaceCharCnt, source));
this._executeCursorEdit(eventsCollector => this.cursor.replacePreviousChar(eventsCollector, text, replaceCharCnt, source));
}
public paste(text: string, pasteOnNewLine: boolean, multicursorText?: string[] | null | undefined, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.paste(eventsCollector, text, pasteOnNewLine, multicursorText, source));
this._executeCursorEdit(eventsCollector => this.cursor.paste(eventsCollector, text, pasteOnNewLine, multicursorText, source));
}
public cut(source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.cut(eventsCollector, source));
this._executeCursorEdit(eventsCollector => this.cursor.cut(eventsCollector, source));
}
public executeCommand(command: ICommand, source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeCommand(eventsCollector, command, source));
this._executeCursorEdit(eventsCollector => this.cursor.executeCommand(eventsCollector, command, source));
}
public executeCommands(commands: ICommand[], source?: string | null | undefined): void {
this._withViewEventsCollector(eventsCollector => this.cursor.executeCommands(eventsCollector, commands, source));
this._executeCursorEdit(eventsCollector => this.cursor.executeCommands(eventsCollector, commands, source));
}
public revealPrimaryCursor(source: string | null | undefined, revealHorizontal: boolean): void {
this._withViewEventsCollector(eventsCollector => this.cursor.revealPrimary(eventsCollector, source, revealHorizontal, ScrollType.Smooth));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册