fix: fix sign rendering for some pre-flatenning worlds

This commit is contained in:
Vitaly 2023-11-11 10:01:27 +03:00
commit 9840e4cd1b
4 changed files with 99 additions and 2 deletions

View file

@ -6,8 +6,9 @@
"start": "node scripts/build.js copyFilesDev && node scripts/prepareData.mjs && node esbuild.mjs --watch",
"start-watch-script": "nodemon -w esbuild.mjs esbuild.mjs",
"build": "node scripts/build.js copyFiles && node scripts/prepareData.mjs -f && node esbuild.mjs --minify --prod",
"check-build": "tsc && pnpm build",
"check-build": "tsc && pnpm test-unit && pnpm build",
"test:cypress": "cypress run",
"test-unit": "vitest",
"test:e2e": "start-test http-get://localhost:8080 test:cypress",
"prod-start": "node server.js",
"postinstall": "node scripts/gen-texturepack-files.mjs && tsx scripts/optimizeBlockCollisions.ts",

View file

@ -57,7 +57,8 @@ export const renderSign = (blockEntity: SignBlockEntity, PrismarineChat: typeof
const defaultColor = ('front_text' in blockEntity ? blockEntity.front_text.color : blockEntity.Color) || 'black'
for (let [lineNum, text] of texts.slice(0, 4).entries()) {
// todo: in pre flatenning it seems the format was not json
const parsed = text?.startsWith('{') ? parseSafe(text ?? '""', 'sign text') : text
if (text === 'null') continue
const parsed = text?.startsWith('{') || text?.startsWith('"') ? parseSafe(text ?? '""', 'sign text') : text
if (!parsed || (typeof parsed !== 'object' && typeof parsed !== 'string')) continue
// todo fix type
const message = typeof parsed === 'string' ? fromFormattedString(parsed) : new PrismarineChat(parsed) as never

View file

@ -0,0 +1,85 @@
import { test, expect } from 'vitest'
import { renderSign } from '.'
import PrismarineChatLoader from 'prismarine-chat'
const PrismarineChat = PrismarineChatLoader({ language: {} } as any)
let ctxTexts = [] as any[]
global.document = {
createElement () {
return {
getContext () {
return {
fillText (text, x, y) {
ctxTexts.push({ text, x, y })
},
measureText () { return 0 }
}
}
}
}
} as any
const render = (entity) => {
ctxTexts = []
renderSign(entity, PrismarineChat)
return ctxTexts.map(({ text, y }) => [y / 80, text])
}
test('sign renderer', () => {
let blockEntity = {
"GlowingText": 0,
"Color": "black",
"Text4": "{\"text\":\"\"}",
"Text3": "{\"text\":\"\"}",
"Text2": "{\"text\":\"\"}",
"Text1": "{\"extra\":[{\"color\":\"dark_green\",\"text\":\"Minecraft \"},{\"text\":\"Tools\"}],\"text\":\"\"}"
} as any
expect(render(blockEntity)).toMatchInlineSnapshot(`
[
[
1,
"",
],
[
1,
"Minecraft ",
],
[
1,
"Tools",
],
[
2,
"",
],
[
3,
"",
],
[
4,
"",
],
]
`)
blockEntity = { // pre flatenning
"Text1": "Welcome to",
"Text2": "",
"Text3": "null",
"Text4": "\"Version 2.1\"",
} as const
expect(render(blockEntity)).toMatchInlineSnapshot(`
[
[
1,
"Welcome to",
],
[
4,
"Version 2.1",
],
]
`)
})

10
vitest.config.ts Normal file
View file

@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
root: 'prismarine-viewer/viewer',
test: {
include: [
'**/*.test.ts'
],
},
})