mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
refactor(editor): remove assertExists (#10615)
This commit is contained in:
@@ -10,23 +10,6 @@ export function isPrimitive(
|
||||
|
||||
export function assertType<T>(_: unknown): asserts _ is T {}
|
||||
|
||||
/**
|
||||
* @deprecated Avoid using this util as escape hatch of error handling.
|
||||
* For non-framework code, please handle error in application level instead.
|
||||
*/
|
||||
export function assertExists<T>(
|
||||
val: T | null | undefined,
|
||||
message: string | Error = 'val does not exist',
|
||||
errorCode = ErrorCode.ValueNotExists
|
||||
): asserts val is T {
|
||||
if (val === null || val === undefined) {
|
||||
if (message instanceof Error) {
|
||||
throw message;
|
||||
}
|
||||
throw new BlockSuiteError(errorCode, message);
|
||||
}
|
||||
}
|
||||
|
||||
export function assertNotExists<T>(
|
||||
val: T | null | undefined,
|
||||
message = 'val exists',
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { html, LitElement, type TemplateResult } from 'lit';
|
||||
import { property } from 'lit/decorators.js';
|
||||
import { styleMap } from 'lit/directives/style-map.js';
|
||||
@@ -14,12 +13,19 @@ export class VLine extends LitElement {
|
||||
const rootElement = this.closest(
|
||||
`[${INLINE_ROOT_ATTR}]`
|
||||
) as InlineRootElement;
|
||||
assertExists(rootElement, 'v-line must be inside a v-root');
|
||||
if (!rootElement) {
|
||||
throw new BlockSuiteError(
|
||||
BlockSuiteError.ErrorCode.ValueNotExists,
|
||||
'v-line must be inside a v-root'
|
||||
);
|
||||
}
|
||||
const inlineEditor = rootElement.inlineEditor;
|
||||
assertExists(
|
||||
inlineEditor,
|
||||
'v-line must be inside a v-root with inline-editor'
|
||||
);
|
||||
if (!inlineEditor) {
|
||||
throw new BlockSuiteError(
|
||||
BlockSuiteError.ErrorCode.ValueNotExists,
|
||||
'v-line must be inside a v-root with inline-editor'
|
||||
);
|
||||
}
|
||||
|
||||
return inlineEditor;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { assertInstanceOf } from '@blocksuite/global/utils';
|
||||
import { effect } from '@preact/signals-core';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
@@ -47,7 +47,7 @@ export class RangeService<TextAttributes extends BaseTextAttributes> {
|
||||
return null;
|
||||
}
|
||||
const textNode = text.childNodes[1];
|
||||
assertExists(textNode instanceof Text);
|
||||
assertInstanceOf(textNode, Text);
|
||||
range.setStart(textNode, 0);
|
||||
range.setEnd(textNode, textNode.textContent?.length ?? 0);
|
||||
const inlineRange = this.toInlineRange(range);
|
||||
@@ -158,7 +158,10 @@ export class RangeService<TextAttributes extends BaseTextAttributes> {
|
||||
// can not in the first line because if we apply the inline ranage manually the
|
||||
// cursor will jump to the second line.
|
||||
const container = range.commonAncestorContainer.parentElement;
|
||||
assertExists(container);
|
||||
if (!container) {
|
||||
console.error('failed to get container');
|
||||
return false;
|
||||
}
|
||||
const containerRect = container.getBoundingClientRect();
|
||||
// There will be two rects if the cursor is at the edge of the line:
|
||||
// aaaaaaaa| or aaaaaaaa
|
||||
@@ -200,7 +203,10 @@ export class RangeService<TextAttributes extends BaseTextAttributes> {
|
||||
// can not in the first line because if we apply the inline range manually the
|
||||
// cursor will jump to the second line.
|
||||
const container = range.commonAncestorContainer.parentElement;
|
||||
assertExists(container);
|
||||
if (!container) {
|
||||
console.error('failed to get container');
|
||||
return false;
|
||||
}
|
||||
const containerRect = container.getBoundingClientRect();
|
||||
// There will be two rects if the cursor is at the edge of the line:
|
||||
// aaaaaaaa| or aaaaaaaa
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { html, render } from 'lit';
|
||||
import { repeat } from 'lit/directives/repeat.js';
|
||||
import * as Y from 'yjs';
|
||||
@@ -36,7 +35,10 @@ export class RenderService<TextAttributes extends BaseTextAttributes> {
|
||||
if (!lastStartRelativePosition || !lastEndRelativePosition) return;
|
||||
|
||||
const doc = this.editor.yText.doc;
|
||||
assertExists(doc);
|
||||
if (!doc) {
|
||||
console.error('doc is not found when syncing yText');
|
||||
return;
|
||||
}
|
||||
const absoluteStart = Y.createAbsolutePositionFromRelativePosition(
|
||||
lastStartRelativePosition,
|
||||
doc
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
ParagraphBlockSchemaExtension,
|
||||
RootBlockSchemaExtension,
|
||||
} from './test-schema.js';
|
||||
import { assertExists } from './test-utils-dom.js';
|
||||
|
||||
function createTestOptions() {
|
||||
const idGenerator = createAutoIncrementIdGenerator();
|
||||
@@ -207,7 +206,9 @@ describe('basic', () => {
|
||||
const doc2 = collection2.getDoc('space:0', {
|
||||
extensions,
|
||||
});
|
||||
assertExists(doc2);
|
||||
if (!doc2) {
|
||||
throw new Error('doc2 is not found');
|
||||
}
|
||||
applyUpdate(doc2.spaceDoc, update);
|
||||
expect(serializCollection(collection2.doc)['spaces']).toEqual({
|
||||
'space:0': {
|
||||
|
||||
@@ -65,13 +65,6 @@ export async function runOnce() {
|
||||
testCases = [];
|
||||
}
|
||||
|
||||
// XXX: workaround typing issue in blobs/__tests__/test-entry.ts
|
||||
export function assertExists<T>(val: T | null | undefined): asserts val is T {
|
||||
if (val === null || val === undefined) {
|
||||
throw new Error('val does not exist');
|
||||
}
|
||||
}
|
||||
|
||||
export async function nextFrame() {
|
||||
return new Promise(resolve => requestAnimationFrame(resolve));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import { diffUpdate, encodeStateVectorFromUpdate, mergeUpdates } from 'yjs';
|
||||
|
||||
import { MANUALLY_STOP } from '../../utils/throw-if-aborted.js';
|
||||
@@ -66,7 +65,12 @@ export class BroadcastChannelDocSource implements DocSource {
|
||||
this.docMap.set(docId, data);
|
||||
}
|
||||
|
||||
assertExists(this.docMap.get(docId));
|
||||
const doc = this.docMap.get(docId);
|
||||
if (!doc) {
|
||||
console.error('data is not found when syncing broadcast channel');
|
||||
return;
|
||||
}
|
||||
|
||||
this.channel.postMessage({
|
||||
type: 'update',
|
||||
docId,
|
||||
|
||||
Reference in New Issue
Block a user