From 4ec9af11d724343cd823395829e0fb994fccf1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Fri, 26 Apr 2024 15:31:22 +0200 Subject: [PATCH] Move to a separate file. --- packages/editor/src/lib/editor/Editor.ts | 20 ++++----- .../lib/editor/types/editor-result-types.ts | 41 +++++++++++++++++++ .../editor/src/lib/editor/types/misc-types.ts | 41 ------------------- 3 files changed, 50 insertions(+), 52 deletions(-) create mode 100644 packages/editor/src/lib/editor/types/editor-result-types.ts diff --git a/packages/editor/src/lib/editor/Editor.ts b/packages/editor/src/lib/editor/Editor.ts index cf2a67fb1..b94ef401f 100644 --- a/packages/editor/src/lib/editor/Editor.ts +++ b/packages/editor/src/lib/editor/Editor.ts @@ -120,6 +120,14 @@ import { getStraightArrowInfo } from './shapes/shared/arrow/straight-arrow' import { RootState } from './tools/RootState' import { StateNode, TLStateNodeConstructor } from './tools/StateNode' import { TLContent } from './types/clipboard-types' +import { + CreateShapeError, + EditorResult, + MAX_SHAPES_REACHED_ERROR_ERROR, + NOT_ARRAY_OF_SHAPES_ERROR, + NO_SHAPES_PROVIDED_ERROR, + READONLY_ROOM_ERROR, +} from './types/editor-result-types' import { TLEventMap } from './types/emit-types' import { TLEventInfo, @@ -129,17 +137,7 @@ import { } from './types/event-types' import { TLExternalAssetContent, TLExternalContent } from './types/external-content' import { TLHistoryBatchOptions } from './types/history-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 { OptionalKeys, RequiredKeys, TLSvgOptions } from './types/misc-types' import { TLResizeHandle } from './types/selection-types' /** @public */ diff --git a/packages/editor/src/lib/editor/types/editor-result-types.ts b/packages/editor/src/lib/editor/types/editor-result-types.ts new file mode 100644 index 000000000..ee413e1ad --- /dev/null +++ b/packages/editor/src/lib/editor/types/editor-result-types.ts @@ -0,0 +1,41 @@ +// Result types +export type Ok = { readonly ok: true } +export type OkWithValue = { readonly ok: true; readonly value: T } +export type Error = { readonly ok: false; readonly error: E } + +export type EditorResult = Error | Ok | OkWithValue +export const EditorResult = { + ok(): Ok { + return { ok: true } + }, + okWithValue(value: T): OkWithValue { + return { ok: true, value } + }, + error(error: E): Error { + return { ok: false, error } + }, +} + +// General errors +export const READONLY_ROOM_ERROR = { type: 'readonly-room' as const, message: 'Room is readonly' } + +// 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 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 } diff --git a/packages/editor/src/lib/editor/types/misc-types.ts b/packages/editor/src/lib/editor/types/misc-types.ts index 6b523b5c8..6851e726d 100644 --- a/packages/editor/src/lib/editor/types/misc-types.ts +++ b/packages/editor/src/lib/editor/types/misc-types.ts @@ -14,44 +14,3 @@ export type TLSvgOptions = { darkMode?: boolean preserveAspectRatio: React.SVGAttributes['preserveAspectRatio'] } - -// General errors -export const READONLY_ROOM_ERROR = { type: 'readonly-room' as const, message: 'Room is readonly' } - -// 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 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 Ok = { readonly ok: true } -export type OkWithValue = { readonly ok: true; readonly value: T } -export type Error = { readonly ok: false; readonly error: E } - -export type EditorResult = Error | Ok | OkWithValue -export const EditorResult = { - ok(): Ok { - return { ok: true } - }, - okWithValue(value: T): OkWithValue { - return { ok: true, value } - }, - error(error: E): Error { - return { ok: false, error } - }, -}