fix: applying 1-width at odd location

This commit is contained in:
Shibo Lyu 2024-12-29 22:32:59 +08:00
parent 5077995eb3
commit 91db478fe9
3 changed files with 22 additions and 12 deletions

View file

@ -44,7 +44,7 @@ export function applyChange(change: BoardChange, section: SectionData) {
if (change.ch) {
const chWidth = getCharacterWidth(change.ch);
const xCharacterOffset = xInSection % 2;
const xCharacterOffset = xInSection % chWidth;
const offsetAdjustedXInSection = xInSection - xCharacterOffset;
section.ch[yInSection][offsetAdjustedXInSection] = change.ch;
section.width[yInSection][offsetAdjustedXInSection] = chWidth;

View file

@ -51,11 +51,13 @@ Deno.test("board", async (t) => {
applyChangeOnBoard({ x: 4, y: 0, ch: "B" }, board);
applyChangeOnBoard({ x: 0, y: 3, ch: "C" }, board);
applyChangeOnBoard({ x: 4, y: 3, ch: "D" }, board);
applyChangeOnBoard({ x: 5, y: 3, ch: "E" }, board);
assertEquals(board.sections[0][0].ch[0][0], "A");
assertEquals(board.sections[0][1].ch[0][0], "B");
assertEquals(board.sections[1][0].ch[0][0], "C");
assertEquals(board.sections[1][1].ch[0][0], "D");
assertEquals(board.sections[1][1].ch[0][1], "E");
applyChangeOnBoard({ x: 0, y: 1, ch: "你" }, board);
applyChangeOnBoard({ x: 4, y: 2, ch: "好" }, board);
@ -65,7 +67,10 @@ Deno.test("board", async (t) => {
assertEquals(board.sections[0][0].ch[1][0], "你");
assertEquals(board.sections[0][1].ch[2][0], "好");
assertEquals(board.sections[1][0].ch[1][0], "嗎");
assertEquals(board.sections[1][1].ch[2][0], "嘛");
assertEquals(board.sections[1][1].ch[2], ["嘛", "E", " ", " "]);
applyChangeOnBoard({ x: 5, y: 4, ch: "啊" }, board);
assertEquals(board.sections[1][1].ch[2], ["啊", "E", " ", " "]);
});
await t.step("getSectionOnBoard: existing section", () => {
@ -74,7 +79,7 @@ Deno.test("board", async (t) => {
const section = getSectionOnBoard({ sx: 1, sy: 1 }, board, {
readOnly: true,
});
assertEquals(section.ch[0][0], "");
assertEquals(section.ch[0][0], "");
assertEquals(section.color[0][0], "F");
assertEquals(section.bgColor[0][0], "0");
assertEquals(section.width[0][0], 2);

View file

@ -90,27 +90,32 @@ Deno.test("section", async (t) => {
assert(section);
applyChange({ x: 0, y: 0, ch: "t" }, section);
assertEquals(section.ch[0][0], "t");
assertEquals(section.ch[0][1], " ");
assertEquals(section.width[0][0], 1);
assertEquals(section.ch[0], ["t", " ", " ", " "]);
assertEquals(section.width[0], [1, 1, 1, 1]);
});
await t.step("applyChange 1-width at odd position", () => {
assert(section);
applyChange({ x: 1, y: 0, ch: "t" }, section);
assertEquals(section.ch[0], ["t", "t", " ", " "]);
assertEquals(section.width[0], [1, 1, 1, 1]);
});
await t.step("applyChange 2-width at a correct position", () => {
assert(section);
applyChange({ x: 0, y: 0, ch: "あ" }, section);
assertEquals(section.ch[0][0], "あ");
assertEquals(section.ch[0][1], " ");
assertEquals(section.width[0][0], 2);
assertEquals(section.ch[0], ["あ", "t", " ", " "]);
assertEquals(section.width[0], [2, 1, 1, 1]);
});
await t.step("applyChange 2-width at an alternate position", () => {
assert(section);
applyChange({ x: 1, y: 0, ch: "あ" }, section);
assertEquals(section.ch[0][0], "あ");
assertEquals(section.ch[0][1], " ");
assertEquals(section.width[0][0], 2);
assertEquals(section.ch[0], ["あ", "t", " ", " "]);
assertEquals(section.width[0], [2, 1, 1, 1]);
});
await t.step("applyChange incorrect section", () => {