Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom <nobr> tag #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"scripts": {
"tsc": "tsc",
"lint": "tslint -c ./tslint.json --project ./tsconfig.json --format stylish",
"test": "nyc --reporter=html --reporter=text mocha --require ts-node/register --bail src/test/**/*.ts",
"test": "nyc --reporter=html --reporter=text mocha --require ts-node/register --bail src/test/**/*.ts src/**/*.test.ts",
"prettier-check": "prettier --list-different \"src/**/*.ts*(x)\"",
"build": "webpack --mode production --devtool source-map --progress",
"start": "NODE_OPTIONS=--openssl-legacy-provider webpack-dev-server --mode development --open --devtool cheap-module-source-map",
Expand Down
22 changes: 22 additions & 0 deletions src/ui/questPlay/brAndNobrTags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as assert from "assert";
import { brAndNobrTags } from "./brAndNobrTags";

describe("brAndNobrTags", () => {
it("should split the text by <br> into separate lines", () => {
const texts = ["a", " b <br> c ", "d", "<br>", "c"];
const result = brAndNobrTags(texts);
assert.deepEqual(result, ["a", " b ", " c ", "d", "", "", "c"]);
});

it("should join line with next if it ends with <nobr>", () => {
const texts = ["a", "b<nobr>", "c", "d", "e <nobr> ", "f", "<nobr>"];
const result = brAndNobrTags(texts);
assert.deepEqual(result, ["a", "bc", "d", "e f", ""]);
});

it("should not cut last empty line", () => {
const texts = ["a", ""];
const result = brAndNobrTags(texts);
assert.deepEqual(result, ["a", ""]);
});
});
26 changes: 26 additions & 0 deletions src/ui/questPlay/brAndNobrTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Split the text by <br> into separate lines
* Join line with next if it ends with <nobr>
*/
export function brAndNobrTags(texts: string[]) {
const out: string[] = [];
let currentLine = "";
let isNeedLastLineFlush = false;
for (const text of texts) {
for (const line of text.split("<br>")) {
const rtimmedLine = line.trimRight();
if (rtimmedLine.endsWith("<nobr>")) {
currentLine += rtimmedLine.slice(0, rtimmedLine.length - "<nobr>".length);
isNeedLastLineFlush = true;
} else {
out.push(currentLine + line);
currentLine = "";
isNeedLastLineFlush = false;
}
}
}
if (isNeedLastLineFlush) {
out.push(currentLine);
}
return out;
}
3 changes: 2 additions & 1 deletion src/ui/questPlay/questPlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { GamePlayButton } from "./questPlay.button";
import { useDarkTheme } from "./questPlay.metatheme";
import { toggleFullscreen } from "./fullscreen";
import { brAndNobrTags } from "./brAndNobrTags";

function initRandomGame(quest: Quest, doFirstStep: boolean) {
const gameState = initGame(
Expand Down Expand Up @@ -282,7 +283,7 @@ export function QuestPlay({
</DivFadeinCss>
);

const paramsStrings = ([] as string[]).concat(...uistate.paramsState.map((x) => x.split("<br>")));
const paramsStrings = brAndNobrTags(uistate.paramsState);

const params = (
<>
Expand Down