chore: fix eslint in blocksuite (#9232)

This commit is contained in:
Saul-Mirone
2024-12-20 16:48:10 +00:00
parent bfcc53dc1f
commit 3a82da0e5b
269 changed files with 935 additions and 842 deletions

View File

@@ -60,10 +60,10 @@ export function onlyContainImgElement(
export class Clipboard extends LifeCycleWatcher {
static override readonly key = 'clipboard';
private _adapterMap: AdapterMap = new Map();
private readonly _adapterMap: AdapterMap = new Map();
// Need to be cloned to a map for later use
private _getDataByType = (clipboardData: DataTransfer) => {
private readonly _getDataByType = (clipboardData: DataTransfer) => {
const data = new Map<string, string | File[]>();
for (const type of clipboardData.types) {
if (type === 'Files') {
@@ -105,7 +105,7 @@ export class Clipboard extends LifeCycleWatcher {
};
};
private _getSnapshotByPriority = async (
private readonly _getSnapshotByPriority = async (
getItem: (type: string) => string | File[],
doc: Doc,
parent?: string,

View File

@@ -128,9 +128,9 @@ import type {
export class CommandManager extends LifeCycleWatcher {
static override readonly key = 'commandManager';
private _commands = new Map<string, Command>();
private readonly _commands = new Map<string, Command>();
private _createChain = (
private readonly _createChain = (
methods: Record<BlockSuite.CommandName, unknown>,
_cmds: Command[]
): Chain => {
@@ -239,7 +239,7 @@ export class CommandManager extends LifeCycleWatcher {
} as Chain;
};
private _getCommandCtx = (): InitCommandCtx => {
private readonly _getCommandCtx = (): InitCommandCtx => {
return {
std: this.std,
};

View File

@@ -4,7 +4,7 @@ import { ClipboardEventState } from '../state/clipboard.js';
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
export class ClipboardControl {
private _copy = (event: ClipboardEvent) => {
private readonly _copy = (event: ClipboardEvent) => {
const clipboardEventState = new ClipboardEventState({
event,
});
@@ -14,7 +14,7 @@ export class ClipboardControl {
);
};
private _cut = (event: ClipboardEvent) => {
private readonly _cut = (event: ClipboardEvent) => {
const clipboardEventState = new ClipboardEventState({
event,
});
@@ -24,7 +24,7 @@ export class ClipboardControl {
);
};
private _paste = (event: ClipboardEvent) => {
private readonly _paste = (event: ClipboardEvent) => {
const clipboardEventState = new ClipboardEventState({
event,
});
@@ -35,7 +35,7 @@ export class ClipboardControl {
);
};
constructor(private _dispatcher: UIEventDispatcher) {}
constructor(private readonly _dispatcher: UIEventDispatcher) {}
private _createContext(event: Event, clipboardState: ClipboardEventState) {
return UIEventStateContext.from(

View File

@@ -11,7 +11,7 @@ import { KeyboardEventState } from '../state/index.js';
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
export class KeyboardControl {
private _down = (event: KeyboardEvent) => {
private readonly _down = (event: KeyboardEvent) => {
if (!this._shouldTrigger(event)) {
return;
}
@@ -25,7 +25,7 @@ export class KeyboardControl {
);
};
private _shouldTrigger = (event: KeyboardEvent) => {
private readonly _shouldTrigger = (event: KeyboardEvent) => {
if (event.isComposing) {
return false;
}
@@ -41,7 +41,7 @@ export class KeyboardControl {
return true;
};
private _up = (event: KeyboardEvent) => {
private readonly _up = (event: KeyboardEvent) => {
if (!this._shouldTrigger(event)) {
return;
}
@@ -58,7 +58,7 @@ export class KeyboardControl {
private composition = false;
constructor(private _dispatcher: UIEventDispatcher) {}
constructor(private readonly _dispatcher: UIEventDispatcher) {}
private _createContext(event: Event, keyboardState: KeyboardEventState) {
return UIEventStateContext.from(

View File

@@ -39,7 +39,7 @@ abstract class PointerControllerBase {
}
class PointerEventForward extends PointerControllerBase {
private _down = (event: PointerEvent) => {
private readonly _down = (event: PointerEvent) => {
const { pointerId } = event;
const pointerState = new PointerEventState({
@@ -54,9 +54,9 @@ class PointerEventForward extends PointerControllerBase {
this._dispatcher.run('pointerDown', createContext(event, pointerState));
};
private _lastStates = new Map<PointerId, PointerEventState>();
private readonly _lastStates = new Map<PointerId, PointerEventState>();
private _move = (event: PointerEvent) => {
private readonly _move = (event: PointerEvent) => {
const { pointerId } = event;
const start = this._startStates.get(pointerId) ?? null;
@@ -74,9 +74,9 @@ class PointerEventForward extends PointerControllerBase {
this._dispatcher.run('pointerMove', createContext(event, state));
};
private _startStates = new Map<PointerId, PointerEventState>();
private readonly _startStates = new Map<PointerId, PointerEventState>();
private _upOrOut = (up: boolean) => (event: PointerEvent) => {
private readonly _upOrOut = (up: boolean) => (event: PointerEvent) => {
const { pointerId } = event;
const start = this._startStates.get(pointerId) ?? null;
@@ -109,7 +109,7 @@ class PointerEventForward extends PointerControllerBase {
}
class ClickController extends PointerControllerBase {
private _down = (event: PointerEvent) => {
private readonly _down = (event: PointerEvent) => {
// disable for secondary pointer
if (event.isPrimary === false) return;
@@ -137,7 +137,7 @@ class ClickController extends PointerControllerBase {
private _pointerDownCount = 0;
private _up = (event: PointerEvent) => {
private readonly _up = (event: PointerEvent) => {
if (!this._downPointerState) return;
if (isFarEnough(this._downPointerState.raw, event)) {
@@ -177,7 +177,7 @@ class ClickController extends PointerControllerBase {
}
class DragController extends PointerControllerBase {
private _down = (event: PointerEvent) => {
private readonly _down = (event: PointerEvent) => {
if (this._nativeDragging) return;
if (!event.isPrimary) {
@@ -209,7 +209,7 @@ class DragController extends PointerControllerBase {
private _lastPointerState: PointerEventState | null = null;
private _move = (event: PointerEvent) => {
private readonly _move = (event: PointerEvent) => {
if (
this._startPointerState === null ||
this._startPointerState.raw.pointerId !== event.pointerId
@@ -243,7 +243,7 @@ class DragController extends PointerControllerBase {
}
};
private _nativeDragEnd = (event: DragEvent) => {
private readonly _nativeDragEnd = (event: DragEvent) => {
this._nativeDragging = false;
const dndEventState = new DndEventState({ event });
this._dispatcher.run(
@@ -254,7 +254,7 @@ class DragController extends PointerControllerBase {
private _nativeDragging = false;
private _nativeDragMove = (event: DragEvent) => {
private readonly _nativeDragMove = (event: DragEvent) => {
const dndEventState = new DndEventState({ event });
this._dispatcher.run(
'nativeDragMove',
@@ -262,7 +262,7 @@ class DragController extends PointerControllerBase {
);
};
private _nativeDragStart = (event: DragEvent) => {
private readonly _nativeDragStart = (event: DragEvent) => {
this._reset();
this._nativeDragging = true;
const dndEventState = new DndEventState({ event });
@@ -272,7 +272,7 @@ class DragController extends PointerControllerBase {
);
};
private _nativeDrop = (event: DragEvent) => {
private readonly _nativeDrop = (event: DragEvent) => {
this._reset();
this._nativeDragging = false;
const dndEventState = new DndEventState({ event });
@@ -282,7 +282,7 @@ class DragController extends PointerControllerBase {
);
};
private _reset = () => {
private readonly _reset = () => {
this._dragging = false;
this._startPointerState = null;
this._lastPointerState = null;
@@ -293,7 +293,7 @@ class DragController extends PointerControllerBase {
private _startPointerState: PointerEventState | null = null;
private _up = (event: PointerEvent) => {
private readonly _up = (event: PointerEvent) => {
if (
!this._startPointerState ||
this._startPointerState.raw.pointerId !== event.pointerId
@@ -358,7 +358,7 @@ class DragController extends PointerControllerBase {
}
abstract class DualDragControllerBase extends PointerControllerBase {
private _down = (event: PointerEvent) => {
private readonly _down = (event: PointerEvent) => {
// Another pointer down
if (
this._startPointerStates.primary !== null &&
@@ -394,7 +394,7 @@ abstract class DualDragControllerBase extends PointerControllerBase {
secondary: null,
};
private _move = (event: PointerEvent) => {
private readonly _move = (event: PointerEvent) => {
if (
this._startPointerStates.primary === null ||
this._startPointerStates.secondary === null
@@ -440,7 +440,7 @@ abstract class DualDragControllerBase extends PointerControllerBase {
};
};
private _reset = () => {
private readonly _reset = () => {
this._startPointerStates = {
primary: null,
secondary: null,
@@ -459,7 +459,7 @@ abstract class DualDragControllerBase extends PointerControllerBase {
secondary: null,
};
private _upOrOut = (event: PointerEvent) => {
private readonly _upOrOut = (event: PointerEvent) => {
const { pointerId } = event;
if (
pointerId === this._startPointerStates.primary?.raw.pointerId ||
@@ -542,7 +542,7 @@ class PanController extends DualDragControllerBase {
export class PointerControl {
private _cachedRect: DOMRect | null = null;
private _getRect = () => {
private readonly _getRect = () => {
if (this._cachedRect === null) {
this._updateRect();
}
@@ -553,9 +553,9 @@ export class PointerControl {
// due to potential performance issues
private _pollingInterval: number | null = null;
private controllers: PointerControllerBase[];
private readonly controllers: PointerControllerBase[];
constructor(private _dispatcher: UIEventDispatcher) {
constructor(private readonly _dispatcher: UIEventDispatcher) {
this.controllers = [
new PointerEventForward(_dispatcher, this._getRect),
new ClickController(_dispatcher, this._getRect),

View File

@@ -8,7 +8,7 @@ import type {
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
export class RangeControl {
private _buildScope = (eventName: EventName) => {
private readonly _buildScope = (eventName: EventName) => {
let scope: EventHandlerRunner[] | undefined;
const selection = document.getSelection();
if (selection && selection.rangeCount > 0) {
@@ -23,19 +23,19 @@ export class RangeControl {
return scope;
};
private _compositionEnd = (event: Event) => {
private readonly _compositionEnd = (event: Event) => {
const scope = this._buildScope('compositionEnd');
this._dispatcher.run('compositionEnd', this._createContext(event), scope);
};
private _compositionStart = (event: Event) => {
private readonly _compositionStart = (event: Event) => {
const scope = this._buildScope('compositionStart');
this._dispatcher.run('compositionStart', this._createContext(event), scope);
};
private _compositionUpdate = (event: Event) => {
private readonly _compositionUpdate = (event: Event) => {
const scope = this._buildScope('compositionUpdate');
this._dispatcher.run(
@@ -47,7 +47,7 @@ export class RangeControl {
private _prev: Range | null = null;
private _selectionChange = (event: Event) => {
private readonly _selectionChange = (event: Event) => {
const selection = document.getSelection();
if (!selection) return;
@@ -59,7 +59,7 @@ export class RangeControl {
this._dispatcher.run('selectionChange', this._createContext(event), scope);
};
constructor(private _dispatcher: UIEventDispatcher) {}
constructor(private readonly _dispatcher: UIEventDispatcher) {}
private _buildEventScopeByNativeRange(name: EventName, range: Range) {
const blockIds = this._findBlockComponentPath(range);

View File

@@ -80,17 +80,17 @@ export class UIEventDispatcher extends LifeCycleWatcher {
private _active = false;
private _clipboardControl: ClipboardControl;
private readonly _clipboardControl: ClipboardControl;
private _handlersMap = Object.fromEntries(
eventNames.map((name): [EventName, Array<EventHandlerRunner>] => [name, []])
) as Record<EventName, Array<EventHandlerRunner>>;
private _keyboardControl: KeyboardControl;
private readonly _keyboardControl: KeyboardControl;
private _pointerControl: PointerControl;
private readonly _pointerControl: PointerControl;
private _rangeControl: RangeControl;
private readonly _rangeControl: RangeControl;
bindHotkey = (...args: Parameters<KeyboardControl['bindHotkey']>) =>
this._keyboardControl.bindHotkey(...args);

View File

@@ -35,7 +35,7 @@ import { Viewport } from './viewport.js';
export class GfxController extends LifeCycleWatcher {
static override key = gfxControllerKey;
private _disposables: DisposableGroup = new DisposableGroup();
private readonly _disposables: DisposableGroup = new DisposableGroup();
private _surface: SurfaceBlockModel | null = null;

View File

@@ -66,16 +66,22 @@ const typeFilters = {
type FilterFunc = (model: GfxModel | GfxLocalElementModel) => boolean;
export class GridManager {
private _elementToGrids = new Map<
private readonly _elementToGrids = new Map<
GfxModel | GfxLocalElementModel,
Set<Set<GfxModel | GfxLocalElementModel>>
>();
private _externalElementToGrids = new Map<GfxModel, Set<Set<GfxModel>>>();
private readonly _externalElementToGrids = new Map<
GfxModel,
Set<Set<GfxModel>>
>();
private _externalGrids = new Map<string, Set<GfxModel>>();
private readonly _externalGrids = new Map<string, Set<GfxModel>>();
private _grids = new Map<string, Set<GfxModel | GfxLocalElementModel>>();
private readonly _grids = new Map<
string,
Set<GfxModel | GfxLocalElementModel>
>();
get isEmpty() {
return this._grids.size === 0;

View File

@@ -4,7 +4,7 @@ import { Signal } from '@preact/signals-core';
import type { BlockStdScope } from '../scope/block-std-scope.js';
export class KeyboardController {
private _disposable = new DisposableGroup();
private readonly _disposable = new DisposableGroup();
shiftKey$ = new Signal<boolean>(false);

View File

@@ -71,7 +71,7 @@ export type Layer = BlockLayer | CanvasLayer;
export class LayerManager {
static INITIAL_INDEX = 'a0';
private _disposable = new DisposableGroup();
private readonly _disposable = new DisposableGroup();
blocks: GfxBlockElementModel[] = [];
@@ -100,7 +100,7 @@ export class LayerManager {
};
constructor(
private _doc: Doc,
private readonly _doc: Doc,
private _surface: SurfaceBlockModel | null,
options: {
watch: boolean;

View File

@@ -388,7 +388,7 @@ export abstract class GfxGroupLikeElementModel<
{
private _childIds: string[] = [];
private _mutex = createMutex();
private readonly _mutex = createMutex();
abstract children: Y.Map<any>;

View File

@@ -39,7 +39,7 @@ export function prop<V, T extends GfxLocalElementModel>() {
}
export abstract class GfxLocalElementModel implements GfxCompatibleInterface {
private _mutex: mutex.mutex = mutex.createMutex();
private readonly _mutex: mutex.mutex = mutex.createMutex();
protected _local = new Map<string | symbol, unknown>();

View File

@@ -83,15 +83,15 @@ export const eventTarget = Symbol('eventTarget');
export class ToolController extends GfxExtension {
static override key = 'ToolController';
private _builtInHookSlot = new Slot<BuiltInSlotContext>();
private readonly _builtInHookSlot = new Slot<BuiltInSlotContext>();
private _disposableGroup = new DisposableGroup();
private readonly _disposableGroup = new DisposableGroup();
private _toolOption$ = new Signal<GfxToolsFullOptionValue>(
private readonly _toolOption$ = new Signal<GfxToolsFullOptionValue>(
{} as GfxToolsFullOptionValue
);
private _tools = new Map<string, BaseTool>();
private readonly _tools = new Map<string, BaseTool>();
readonly currentToolName$ = new Signal<keyof GfxToolsMap>();

View File

@@ -15,11 +15,11 @@ import {
export class ViewManager extends GfxExtension {
static override key = 'viewManager';
private _disposable = new DisposableGroup();
private readonly _disposable = new DisposableGroup();
private _viewCtorMap = new Map<string, typeof GfxElementModelView>();
private readonly _viewCtorMap = new Map<string, typeof GfxElementModelView>();
private _viewMap = new Map<string, GfxElementModelView>();
private readonly _viewMap = new Map<string, GfxElementModelView>();
constructor(gfx: GfxController) {
super(gfx);

View File

@@ -40,7 +40,10 @@ export class GfxElementModelView<
{
static type: string;
private _handlers = new Map<keyof EventsHandlerMap, ((evt: any) => void)[]>();
private readonly _handlers = new Map<
keyof EventsHandlerMap,
((evt: any) => void)[]
>();
private _isConnected = true;

View File

@@ -46,7 +46,7 @@ export class GfxViewportElement extends WithDisposable(ShadowlessElement) {
}
`;
private _hideOutsideBlock = requestThrottledConnectedFrame(() => {
private readonly _hideOutsideBlock = requestThrottledConnectedFrame(() => {
if (this.getModelsInViewport && this.host) {
const host = this.host;
const modelsInViewport = this.getModelsInViewport();
@@ -77,12 +77,12 @@ export class GfxViewportElement extends WithDisposable(ShadowlessElement) {
private _lastVisibleModels?: Set<GfxBlockElementModel>;
private _pendingChildrenUpdates: {
private readonly _pendingChildrenUpdates: {
id: string;
resolve: () => void;
}[] = [];
private _refreshViewport = requestThrottledConnectedFrame(() => {
private readonly _refreshViewport = requestThrottledConnectedFrame(() => {
this._hideOutsideBlock();
}, this);

View File

@@ -15,7 +15,7 @@ export class RangeBinding {
| ((event: CompositionEvent) => Promise<void>)
| null = null;
private _computePath = (modelId: string) => {
private readonly _computePath = (modelId: string) => {
const block = this.host.std.doc.getBlock(modelId)?.model;
if (!block) return [];
@@ -29,7 +29,7 @@ export class RangeBinding {
return path;
};
private _onBeforeInput = (event: InputEvent) => {
private readonly _onBeforeInput = (event: InputEvent) => {
const selection = this.selectionManager.find('text');
if (!selection) return;
@@ -85,7 +85,7 @@ export class RangeBinding {
this.selectionManager.setGroup('note', [newSelection]);
};
private _onCompositionEnd = (event: CompositionEvent) => {
private readonly _onCompositionEnd = (event: CompositionEvent) => {
if (this._compositionStartCallback) {
event.preventDefault();
event.stopPropagation();
@@ -94,7 +94,7 @@ export class RangeBinding {
}
};
private _onCompositionStart = () => {
private readonly _onCompositionStart = () => {
const selection = this.selectionManager.find('text');
if (!selection) return;
@@ -166,7 +166,7 @@ export class RangeBinding {
};
};
private _onNativeSelectionChanged = async () => {
private readonly _onNativeSelectionChanged = async () => {
if (this.isComposing) return;
if (!this.host) return; // Unstable when switching views, card <-> embed
@@ -246,7 +246,7 @@ export class RangeBinding {
this.rangeManager?.syncRangeToTextSelection(range, isRangeReversed);
};
private _onStdSelectionChanged = (selections: BaseSelection[]) => {
private readonly _onStdSelectionChanged = (selections: BaseSelection[]) => {
const text =
selections.find((selection): selection is TextSelection =>
selection.is('text')

View File

@@ -18,20 +18,20 @@ export interface SelectionConstructor {
export class SelectionManager extends LifeCycleWatcher {
static override readonly key = 'selectionManager';
private _id: string;
private readonly _id: string;
private _itemAdded = (event: { stackItem: StackItem }) => {
private readonly _itemAdded = (event: { stackItem: StackItem }) => {
event.stackItem.meta.set('selection-state', this.value);
};
private _itemPopped = (event: { stackItem: StackItem }) => {
private readonly _itemPopped = (event: { stackItem: StackItem }) => {
const selection = event.stackItem.meta.get('selection-state');
if (selection) {
this.set(selection as BaseSelection[]);
}
};
private _jsonToSelection = (json: Record<string, unknown>) => {
private readonly _jsonToSelection = (json: Record<string, unknown>) => {
const ctor = this._selectionConstructors[json.type as string];
if (!ctor) {
throw new BlockSuiteError(
@@ -42,11 +42,13 @@ export class SelectionManager extends LifeCycleWatcher {
return ctor.fromJSON(json);
};
private _remoteSelections = signal<Map<number, BaseSelection[]>>(new Map());
private readonly _remoteSelections = signal<Map<number, BaseSelection[]>>(
new Map()
);
private _selectionConstructors: Record<string, SelectionConstructor> = {};
private _selections = signal<BaseSelection[]>([]);
private readonly _selections = signal<BaseSelection[]>([]);
disposables = new DisposableGroup();

View File

@@ -35,7 +35,7 @@ export class BlockComponent<
@consume({ context: stdContext })
accessor std!: BlockStdScope;
private _selected = computed(() => {
private readonly _selected = computed(() => {
const selection = this.std.selection.value.find(selection => {
return selection.blockId === this.model?.id;
});

View File

@@ -108,13 +108,13 @@ export abstract class GfxBlockComponent<
const parent = this.parentElement;
if (this.hasUpdated || !parent || !('scheduleUpdateChildren' in parent)) {
super.scheduleUpdate();
return super.scheduleUpdate();
} else {
await (parent.scheduleUpdateChildren as (id: string) => Promise<void>)(
this.model.id
);
super.scheduleUpdate();
return super.scheduleUpdate();
}
}
@@ -207,13 +207,13 @@ export function toGfxBlockComponent<
const parent = this.parentElement;
if (this.hasUpdated || !parent || !('scheduleUpdateChildren' in parent)) {
super.scheduleUpdate();
return super.scheduleUpdate();
} else {
await (parent.scheduleUpdateChildren as (id: string) => Promise<void>)(
this.model.id
);
super.scheduleUpdate();
return super.scheduleUpdate();
}
}

View File

@@ -41,7 +41,7 @@ export class EditorHost extends SignalWatcher(
}
`;
private _renderModel = (model: BlockModel): TemplateResult => {
private readonly _renderModel = (model: BlockModel): TemplateResult => {
const { flavour } = model;
const block = this.doc.getBlock(model.id);
if (!block || block.blockViewType === BlockViewType.Hidden) {

View File

@@ -6,7 +6,7 @@ export class ViewStore extends LifeCycleWatcher {
private readonly _blockMap = new Map<string, BlockComponent>();
private _fromId = (
private readonly _fromId = (
blockId: string | undefined | null
): BlockComponent | null => {
const id = blockId ?? this.std.doc.root?.id;