diff --git a/packages/editor/api-report.md b/packages/editor/api-report.md index bfda225fe..44e7ad3d4 100644 --- a/packages/editor/api-report.md +++ b/packages/editor/api-report.md @@ -633,8 +633,8 @@ export class Editor extends EventEmitter { }; }; createPage(page: Partial): this; - createShape(shape: OptionalKeys, 'id'>): EditorResult; - createShapes(shapes: OptionalKeys, 'id'>[]): EditorResult; + createShape(shape: OptionalKeys, 'id'>): EditorResult; + createShapes(shapes: OptionalKeys, 'id'>[]): EditorResult; deleteAssets(assets: TLAsset[] | TLAssetId[]): this; deleteOpenMenu(id: string): this; deletePage(page: TLPage | TLPageId): this; diff --git a/packages/editor/src/lib/editor/Editor.ts b/packages/editor/src/lib/editor/Editor.ts index ae5ee9e0b..cf2a67fb1 100644 --- a/packages/editor/src/lib/editor/Editor.ts +++ b/packages/editor/src/lib/editor/Editor.ts @@ -129,7 +129,17 @@ import { } from './types/event-types' import { TLExternalAssetContent, TLExternalContent } from './types/external-content' import { TLHistoryBatchOptions } from './types/history-types' -import { EditorResult, OptionalKeys, RequiredKeys, TLSvgOptions } from './types/misc-types' +import { + CreateShapeError, + EditorResult, + MAX_SHAPES_REACHED_ERROR_ERROR, + NOT_ARRAY_OF_SHAPES_ERROR, + NO_SHAPES_PROVIDED_ERROR, + OptionalKeys, + READONLY_ROOM_ERROR, + RequiredKeys, + TLSvgOptions, +} from './types/misc-types' import { TLResizeHandle } from './types/selection-types' /** @public */ @@ -6243,12 +6253,12 @@ export class Editor extends EventEmitter { */ createShapes( shapes: OptionalKeys, 'id'>[] - ): EditorResult { + ): EditorResult { if (!Array.isArray(shapes)) { - return EditorResult.error('not-an-array-of-shapes') + return EditorResult.error(NOT_ARRAY_OF_SHAPES_ERROR) } - if (this.getInstanceState().isReadonly) return EditorResult.error('readonly-room') - if (shapes.length <= 0) return EditorResult.error('no-shapes-provied') + if (this.getInstanceState().isReadonly) return EditorResult.error(READONLY_ROOM_ERROR) + if (shapes.length <= 0) return EditorResult.error(NO_SHAPES_PROVIDED_ERROR) const currentPageShapeIds = this.getCurrentPageShapeIds() @@ -6257,7 +6267,7 @@ export class Editor extends EventEmitter { if (maxShapesReached) { // can't create more shapes than fit on the page alertMaxShapes(this) - return EditorResult.error('max-shapes-reached') + return EditorResult.error(MAX_SHAPES_REACHED_ERROR_ERROR) } const focusedGroupId = this.getFocusedGroupId() diff --git a/packages/editor/src/lib/editor/types/misc-types.ts b/packages/editor/src/lib/editor/types/misc-types.ts index befae341f..c1ddbd368 100644 --- a/packages/editor/src/lib/editor/types/misc-types.ts +++ b/packages/editor/src/lib/editor/types/misc-types.ts @@ -15,34 +15,35 @@ export type TLSvgOptions = { preserveAspectRatio: React.SVGAttributes['preserveAspectRatio'] } -export type TLEditorErrorType = keyof typeof TLEditorErrorTypeMap +// General errors +export const READONLY_ROOM_ERROR = { type: 'readonly-room' as const, message: 'Room is readonly' } -export type TLEditorError = { message: string; type: TLEditorErrorType } - -const TLEditorErrorTypeMap = { - 'not-an-array-of-shapes': { - message: 'createShapes requires an array of shapes', - type: 'not-an-array-of-shapes' as const, - }, - 'no-shapes-provied': { - message: 'No shapes provided', - type: 'no-shapes-provied' as const, - }, - 'readonly-room': { - message: 'Room is readonly', - type: 'readonly-room' as const, - }, - 'max-shapes-reached': { - message: 'Max shapes reached', - type: 'max-shapes-reached' as const, - }, +// Create shape errors +export const NOT_ARRAY_OF_SHAPES_ERROR = { + type: 'not-array' as const, + message: 'Expected an array', +} +export const NO_SHAPES_PROVIDED_ERROR = { + type: 'no-shapes-provided' as const, + message: 'No shapes provided', +} +export const MAX_SHAPES_REACHED_ERROR_ERROR = { + type: 'max-shapes-reached' as const, + message: 'Max shapes reached', } -export type ErrorResult = { ok: false; error: TLEditorError } -export type OkResult = { ok: true } -export type OkResultWithValue = { ok: true; value: T } +export type CreateShapeErrorType = + | (typeof READONLY_ROOM_ERROR)['type'] + | (typeof NOT_ARRAY_OF_SHAPES_ERROR)['type'] + | (typeof NO_SHAPES_PROVIDED_ERROR)['type'] + | (typeof MAX_SHAPES_REACHED_ERROR_ERROR)['type'] +export type CreateShapeError = { type: CreateShapeErrorType; message: string } -export type EditorResult = ErrorResult | OkResult | OkResultWithValue +export type OkResult = { readonly ok: true } +export type OkResultWithValue = { readonly ok: true; readonly value: T } +export type ErrorResult = { readonly ok: false; readonly error: E } + +export type EditorResult = ErrorResult | OkResult | OkResultWithValue export const EditorResult = { ok(): OkResult { return { ok: true } @@ -50,7 +51,7 @@ export const EditorResult = { okWithValue(value: T): OkResultWithValue { return { ok: true, value } }, - error(errorType: TLEditorErrorType): ErrorResult { - return { ok: false, error: TLEditorErrorTypeMap[errorType] } + error(error: E): ErrorResult { + return { ok: false, error } }, }