Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
blueset committed Nov 4, 2023
2 parents 24cbdb9 + 23e9aeb commit c9c2fbb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function applyDiff(source: string, edited: string): string {
let relativeTimeOffset = 0; // relative time offset for when line break happens
const finalizeLine = () => {
if (resultTimeTags.length > 0) {
if (timeTagQueue.length > 0) {
if ((timeTagQueue?.length ?? 0) > 0) {
const eolTimeTag = new WordTimeTagLabel(
timeTagQueue[0].timeTag,
timeTagQueue[0].index + resultLine.content.length - ptr
Expand Down Expand Up @@ -118,25 +118,25 @@ function applyDiff(source: string, edited: string): string {
const baseOffset = resultLine.content.length - ptr;
if (text !== "\0") resultLine.content += text;
ptr += text.length;
while (timeTagQueue.length > 0 && timeTagQueue[0]?.index < ptr) {
while ((timeTagQueue?.length ?? 0) > 0 && timeTagQueue[0]?.index < ptr) {
const tag = timeTagQueue.shift();
tag.index += baseOffset;
tag.timeTag += relativeTimeOffset;
resultTimeTags.push(tag);
}
while (
furiganaQueue.length > 0 &&
(furiganaQueue?.length ?? 0) > 0 &&
furiganaQueue[0]?.range[1] <= ptr
) {
const tag = furiganaQueue.shift();
tag.range[0] += baseOffset;
tag.range[1] += baseOffset;
resultFurigana.push(tag);
}
while (dotsQueue.length > 0 && dotsQueue[0][1] <= ptr) {
while ((dotsQueue?.length ?? 0) > 0 && dotsQueue[0][1] <= ptr) {
resultDots.push(dotsQueue.shift()[0]);
}
while (tagsQueue.length > 0 && tagsQueue[0][1] <= ptr) {
while ((tagsQueue?.length ?? 0) > 0 && tagsQueue[0][1] <= ptr) {
resultTags.push(tagsQueue.shift()[0]);
}
} else if (op === 1 && text !== "\n") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@ interface PlainTextHangingPunctProps {
children: string;
}

function shiftinPuncts(line: string, match: RegExpMatchArray | null, start: string, end: string ): RegExpMatchArray | null {
if (match && line.match(new RegExp(`${start}.*${end}`, "g"))) {
const front = match[1].split(start);
const back = match[3].split(end);
match[2] = `${start.repeat(front.length - 1)}${match[2]}${end.repeat(back.length - 1)}`;
match[1] = front.join("");
match[3] = back.join("");
}
return match;
}

export function PlainTextHangingPunct({
children,
}: PlainTextHangingPunctProps) {
const lines = children
.split("\n")
.map((line) => line.match(/^([\p{Ps}\p{Pi}"]*)(.*?)(\p{Po}*)$/u));
.map((line) => {
let match = line.match(/^([\p{Ps}\p{Pi}"]*)(.*?)(\p{Po}*)$/u);
match = shiftinPuncts(line, match, "「", "」");
match = shiftinPuncts(line, match, "『", "』");
match = shiftinPuncts(line, match, "「", "」");
return match;
});
return (
<>
{lines.map((line, idx) => (
Expand Down

0 comments on commit c9c2fbb

Please sign in to comment.