diff --git a/packages/xl-multi-column/src/test/conversions/formatConversionTestUtil.ts b/packages/xl-multi-column/src/test/conversions/formatConversionTestUtil.ts deleted file mode 100644 index 8eea1129df..0000000000 --- a/packages/xl-multi-column/src/test/conversions/formatConversionTestUtil.ts +++ /dev/null @@ -1,199 +0,0 @@ -// TODO: remove duplicate file - -import { - Block, - BlockNoteSchema, - BlockSchema, - InlineContent, - InlineContentSchema, - isPartialLinkInlineContent, - isStyledTextInlineContent, - PartialBlock, - PartialInlineContent, - PartialTableCell, - StyledText, - StyleSchema, - TableCell, - TableContent, - UniqueID, -} from "@blocknote/core"; - -function textShorthandToStyledText( - content: string | StyledText[] = "", -): StyledText[] { - if (typeof content === "string") { - return [ - { - type: "text", - text: content, - styles: {}, - }, - ]; - } - return content; -} - -function partialContentToInlineContent( - content: - | PartialInlineContent - | PartialTableCell - | TableContent - | undefined, -): - | InlineContent[] - | TableContent - | TableCell - | undefined { - if (typeof content === "string") { - return textShorthandToStyledText(content); - } - - if (Array.isArray(content)) { - return content.flatMap((partialContent) => { - if (typeof partialContent === "string") { - return textShorthandToStyledText(partialContent); - } else if (isPartialLinkInlineContent(partialContent)) { - return { - ...partialContent, - content: textShorthandToStyledText(partialContent.content), - }; - } else if (isStyledTextInlineContent(partialContent)) { - return partialContent; - } else { - // custom inline content - - return { - props: {}, - ...partialContent, - content: partialContentToInlineContent(partialContent.content), - } as any; - } - }); - } else if (content?.type === "tableContent") { - return { - type: "tableContent", - columnWidths: content.columnWidths, - headerRows: content.headerRows, - headerCols: content.headerCols, - rows: content.rows.map((row) => ({ - ...row, - cells: row.cells.map( - (cell) => partialContentToInlineContent(cell) as any, - ), - })), - }; - } else if (content?.type === "tableCell") { - return { - type: "tableCell", - content: partialContentToInlineContent(content.content) as any[], - props: { - backgroundColor: content.props?.backgroundColor ?? "default", - textColor: content.props?.textColor ?? "default", - textAlignment: content.props?.textAlignment ?? "left", - colspan: content.props?.colspan ?? 1, - rowspan: content.props?.rowspan ?? 1, - }, - } satisfies TableCell; - } - - return content; -} - -export function partialBlocksToBlocksForTesting< - BSchema extends BlockSchema, - I extends InlineContentSchema, - S extends StyleSchema, ->( - schema: BlockNoteSchema, - partialBlocks: Array, NoInfer, NoInfer>>, -): Array> { - return partialBlocks.map((partialBlock) => - partialBlockToBlockForTesting(schema.blockSchema, partialBlock), - ); -} - -export function partialBlockToBlockForTesting< - BSchema extends BlockSchema, - I extends InlineContentSchema, - S extends StyleSchema, ->( - schema: BSchema, - partialBlock: PartialBlock, -): Block { - const contentType: "inline" | "table" | "none" = - schema[partialBlock.type!].content; - - const withDefaults: Block = { - id: "", - type: partialBlock.type!, - props: {} as any, - content: - contentType === "inline" - ? [] - : contentType === "table" - ? { - type: "tableContent", - columnWidths: undefined, - headerRows: undefined, - headerCols: undefined, - rows: [], - } - : (undefined as any), - children: [] as any, - ...partialBlock, - }; - - Object.entries(schema[partialBlock.type!].propSchema).forEach( - ([propKey, propValue]) => { - if ( - withDefaults.props[propKey] === undefined && - propValue.default !== undefined - ) { - (withDefaults.props as any)[propKey] = propValue.default; - } - }, - ); - - if (contentType === "inline") { - const content = withDefaults.content as InlineContent[] | undefined; - withDefaults.content = partialContentToInlineContent(content) as any; - } else if (contentType === "table") { - const content = withDefaults.content as TableContent | undefined; - withDefaults.content = { - type: "tableContent", - columnWidths: - content?.columnWidths || - content?.rows[0]?.cells.map(() => undefined) || - [], - headerRows: content?.headerRows || undefined, - headerCols: content?.headerCols || undefined, - rows: - content?.rows.map((row) => ({ - cells: row.cells.map((cell) => partialContentToInlineContent(cell)), - })) || [], - } as any; - } - - return { - ...withDefaults, - content: partialContentToInlineContent(withDefaults.content), - children: withDefaults.children.map((c) => { - return partialBlockToBlockForTesting(schema, c); - }), - } as any; -} - -export function addIdsToBlock(block: PartialBlock) { - if (!block.id) { - block.id = UniqueID.options.generateID(); - } - if (block.children) { - addIdsToBlocks(block.children); - } -} - -export function addIdsToBlocks(blocks: PartialBlock[]) { - for (const block of blocks) { - addIdsToBlock(block); - } -} diff --git a/packages/xl-multi-column/src/test/conversions/htmlConversion.test.ts b/packages/xl-multi-column/src/test/conversions/htmlConversion.test.ts index f4e9545ab8..1d70f5621d 100644 --- a/packages/xl-multi-column/src/test/conversions/htmlConversion.test.ts +++ b/packages/xl-multi-column/src/test/conversions/htmlConversion.test.ts @@ -9,12 +9,12 @@ import { createExternalHTMLExporter, createInternalHTMLSerializer, } from "@blocknote/core"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; - import { addIdsToBlocks, partialBlocksToBlocksForTesting, -} from "./formatConversionTestUtil.js"; +} from "@shared/formatConversionTestUtil.js"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + import { multiColumnSchemaTestCases } from "./testCases.js"; // TODO: code same from @blocknote/core, maybe create separate test util package diff --git a/packages/xl-multi-column/src/test/conversions/nodeConversion.test.ts b/packages/xl-multi-column/src/test/conversions/nodeConversion.test.ts index 7e22bd8309..1809b9baaf 100644 --- a/packages/xl-multi-column/src/test/conversions/nodeConversion.test.ts +++ b/packages/xl-multi-column/src/test/conversions/nodeConversion.test.ts @@ -7,8 +7,8 @@ import { blockToNode, nodeToBlock, } from "@blocknote/core"; +import { partialBlockToBlockForTesting } from "@shared/formatConversionTestUtil.js"; -import { partialBlockToBlockForTesting } from "./formatConversionTestUtil.js"; import { multiColumnSchemaTestCases } from "./testCases.js"; function addIdsToBlock(block: PartialBlock) { diff --git a/packages/xl-multi-column/tsconfig.json b/packages/xl-multi-column/tsconfig.json index c74ac34642..a75fe88fa2 100644 --- a/packages/xl-multi-column/tsconfig.json +++ b/packages/xl-multi-column/tsconfig.json @@ -19,7 +19,21 @@ "declarationDir": "types", "composite": true, "skipLibCheck": true, - "emitDeclarationOnly": true + "emitDeclarationOnly": true, + "paths": { + "@shared/*": ["../../shared/*"] + } }, - "include": ["src"] + "include": ["src"], + "references": [ + { + "path": "../core" + }, + { + "path": "../react" + }, + { + "path": "../../shared" + } + ] } diff --git a/packages/xl-multi-column/vite.config.ts b/packages/xl-multi-column/vite.config.ts index ac95b05a82..71d4d4bde7 100644 --- a/packages/xl-multi-column/vite.config.ts +++ b/packages/xl-multi-column/vite.config.ts @@ -20,8 +20,11 @@ export default defineConfig((conf) => ({ resolve: { alias: conf.command === "build" - ? ({} as Record) + ? ({ + "@shared": path.resolve(__dirname, "../../shared/"), + } as Record) : ({ + "@shared": path.resolve(__dirname, "../../shared/"), // load live from sources with live reload working "@blocknote/core": path.resolve(__dirname, "../core/src/"), } as Record), diff --git a/playground/vite.config.ts b/playground/vite.config.ts index c3586999fa..6f1acf1ad0 100644 --- a/playground/vite.config.ts +++ b/playground/vite.config.ts @@ -24,7 +24,7 @@ export default defineConfig((conf) => ({ nesting, tailwindcss("../packages/shadcn/tailwind.config.js"), // Adjust the path as necessary auto, - ], + ] as any, }, // postcss: "../packages/shadcn/postcss.config.js", }, diff --git a/tests/src/unit/shared/formatConversion/export/exportTestExecutors.ts b/tests/src/unit/shared/formatConversion/export/exportTestExecutors.ts index 055218bb22..e7ccef6e5b 100644 --- a/tests/src/unit/shared/formatConversion/export/exportTestExecutors.ts +++ b/tests/src/unit/shared/formatConversion/export/exportTestExecutors.ts @@ -5,10 +5,10 @@ import { InlineContentSchema, StyleSchema, } from "@blocknote/core"; +import { addIdsToBlocks } from "@shared/formatConversionTestUtil.js"; import { prettify } from "htmlfy"; import { expect } from "vitest"; -import { addIdsToBlocks } from "../formatConversionTestUtil.js"; import { ExportTestCase } from "./exportTestCase.js"; export const testExportBlockNoteHTML = async < diff --git a/tests/src/unit/shared/formatConversion/exportParseEquality/exportParseEqualityTestExecutors.ts b/tests/src/unit/shared/formatConversion/exportParseEquality/exportParseEqualityTestExecutors.ts index b340b5f9e6..e9ee8c501c 100644 --- a/tests/src/unit/shared/formatConversion/exportParseEquality/exportParseEqualityTestExecutors.ts +++ b/tests/src/unit/shared/formatConversion/exportParseEquality/exportParseEqualityTestExecutors.ts @@ -6,13 +6,12 @@ import { nodeToBlock, StyleSchema, } from "@blocknote/core"; -import { expect } from "vitest"; - -// TODO: fix import and add lint rule, or allow? import { addIdsToBlocks, partialBlocksToBlocksForTesting, -} from "../formatConversionTestUtil.js"; +} from "@shared/formatConversionTestUtil.js"; +import { expect } from "vitest"; + import { ExportParseEqualityTestCase } from "./exportParseEqualityTestCase.js"; export const testExportParseEqualityBlockNoteHTML = async < diff --git a/tests/src/unit/shared/formatConversion/formatConversionTestUtil.ts b/tests/src/unit/shared/formatConversion/formatConversionTestUtil.ts deleted file mode 100644 index 8eea1129df..0000000000 --- a/tests/src/unit/shared/formatConversion/formatConversionTestUtil.ts +++ /dev/null @@ -1,199 +0,0 @@ -// TODO: remove duplicate file - -import { - Block, - BlockNoteSchema, - BlockSchema, - InlineContent, - InlineContentSchema, - isPartialLinkInlineContent, - isStyledTextInlineContent, - PartialBlock, - PartialInlineContent, - PartialTableCell, - StyledText, - StyleSchema, - TableCell, - TableContent, - UniqueID, -} from "@blocknote/core"; - -function textShorthandToStyledText( - content: string | StyledText[] = "", -): StyledText[] { - if (typeof content === "string") { - return [ - { - type: "text", - text: content, - styles: {}, - }, - ]; - } - return content; -} - -function partialContentToInlineContent( - content: - | PartialInlineContent - | PartialTableCell - | TableContent - | undefined, -): - | InlineContent[] - | TableContent - | TableCell - | undefined { - if (typeof content === "string") { - return textShorthandToStyledText(content); - } - - if (Array.isArray(content)) { - return content.flatMap((partialContent) => { - if (typeof partialContent === "string") { - return textShorthandToStyledText(partialContent); - } else if (isPartialLinkInlineContent(partialContent)) { - return { - ...partialContent, - content: textShorthandToStyledText(partialContent.content), - }; - } else if (isStyledTextInlineContent(partialContent)) { - return partialContent; - } else { - // custom inline content - - return { - props: {}, - ...partialContent, - content: partialContentToInlineContent(partialContent.content), - } as any; - } - }); - } else if (content?.type === "tableContent") { - return { - type: "tableContent", - columnWidths: content.columnWidths, - headerRows: content.headerRows, - headerCols: content.headerCols, - rows: content.rows.map((row) => ({ - ...row, - cells: row.cells.map( - (cell) => partialContentToInlineContent(cell) as any, - ), - })), - }; - } else if (content?.type === "tableCell") { - return { - type: "tableCell", - content: partialContentToInlineContent(content.content) as any[], - props: { - backgroundColor: content.props?.backgroundColor ?? "default", - textColor: content.props?.textColor ?? "default", - textAlignment: content.props?.textAlignment ?? "left", - colspan: content.props?.colspan ?? 1, - rowspan: content.props?.rowspan ?? 1, - }, - } satisfies TableCell; - } - - return content; -} - -export function partialBlocksToBlocksForTesting< - BSchema extends BlockSchema, - I extends InlineContentSchema, - S extends StyleSchema, ->( - schema: BlockNoteSchema, - partialBlocks: Array, NoInfer, NoInfer>>, -): Array> { - return partialBlocks.map((partialBlock) => - partialBlockToBlockForTesting(schema.blockSchema, partialBlock), - ); -} - -export function partialBlockToBlockForTesting< - BSchema extends BlockSchema, - I extends InlineContentSchema, - S extends StyleSchema, ->( - schema: BSchema, - partialBlock: PartialBlock, -): Block { - const contentType: "inline" | "table" | "none" = - schema[partialBlock.type!].content; - - const withDefaults: Block = { - id: "", - type: partialBlock.type!, - props: {} as any, - content: - contentType === "inline" - ? [] - : contentType === "table" - ? { - type: "tableContent", - columnWidths: undefined, - headerRows: undefined, - headerCols: undefined, - rows: [], - } - : (undefined as any), - children: [] as any, - ...partialBlock, - }; - - Object.entries(schema[partialBlock.type!].propSchema).forEach( - ([propKey, propValue]) => { - if ( - withDefaults.props[propKey] === undefined && - propValue.default !== undefined - ) { - (withDefaults.props as any)[propKey] = propValue.default; - } - }, - ); - - if (contentType === "inline") { - const content = withDefaults.content as InlineContent[] | undefined; - withDefaults.content = partialContentToInlineContent(content) as any; - } else if (contentType === "table") { - const content = withDefaults.content as TableContent | undefined; - withDefaults.content = { - type: "tableContent", - columnWidths: - content?.columnWidths || - content?.rows[0]?.cells.map(() => undefined) || - [], - headerRows: content?.headerRows || undefined, - headerCols: content?.headerCols || undefined, - rows: - content?.rows.map((row) => ({ - cells: row.cells.map((cell) => partialContentToInlineContent(cell)), - })) || [], - } as any; - } - - return { - ...withDefaults, - content: partialContentToInlineContent(withDefaults.content), - children: withDefaults.children.map((c) => { - return partialBlockToBlockForTesting(schema, c); - }), - } as any; -} - -export function addIdsToBlock(block: PartialBlock) { - if (!block.id) { - block.id = UniqueID.options.generateID(); - } - if (block.children) { - addIdsToBlocks(block.children); - } -} - -export function addIdsToBlocks(blocks: PartialBlock[]) { - for (const block of blocks) { - addIdsToBlock(block); - } -} diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 1d8b5d9bee..eed3ac7d53 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -19,7 +19,10 @@ "declarationDir": "types", "composite": true, "skipLibCheck": true, - "types": ["node"] + "types": ["node"], + "paths": { + "@shared/*": ["../shared/*"] + } }, "include": ["src"], "references": [ @@ -28,6 +31,9 @@ }, { "path": "../packages/react" + }, + { + "path": "../shared" } ] } diff --git a/tests/vite.config.ts b/tests/vite.config.ts index 785134a22e..b76c27de4a 100644 --- a/tests/vite.config.ts +++ b/tests/vite.config.ts @@ -12,8 +12,11 @@ export default defineConfig((conf) => ({ resolve: { alias: conf.command === "build" - ? ({} as Record) + ? ({ + "@shared": path.resolve(__dirname, "../shared/"), + } as Record) : ({ + "@shared": path.resolve(__dirname, "../shared/"), // load live from sources with live reload working "@blocknote/core": path.resolve(__dirname, "../packages/core/src/"), "@blocknote/react": path.resolve(