2022-01-06 12:17:34 +02:00
|
|
|
import * as wasm from "./pkg";
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
import { Text } from "./text";
|
|
|
|
import { DeleteText } from "./delete-text";
|
|
|
|
import { Tab } from "./tab";
|
|
|
|
import { Break, BreakType } from "./break";
|
2021-03-20 17:16:43 +02:00
|
|
|
import { BorderType } from "./border";
|
2022-05-23 10:24:44 +03:00
|
|
|
import { Image } from "./image";
|
2024-10-15 12:30:05 +03:00
|
|
|
import { PositionalTab } from "./positional-tab";
|
2024-11-22 03:58:49 +02:00
|
|
|
import {
|
|
|
|
createDefaultRunProperty,
|
|
|
|
RunFonts,
|
|
|
|
RunProperty,
|
|
|
|
setRunProperty,
|
|
|
|
VertAlignType,
|
|
|
|
} from "./run-property";
|
2020-01-30 16:14:25 +02:00
|
|
|
|
2024-10-15 12:30:05 +03:00
|
|
|
export type RunChild = Text | DeleteText | Tab | Break | Image | PositionalTab;
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
export class Run {
|
|
|
|
children: RunChild[] = [];
|
2024-11-22 03:58:49 +02:00
|
|
|
property: RunProperty;
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
addText(text: string) {
|
|
|
|
this.children.push(new Text(text));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-05-23 10:24:44 +03:00
|
|
|
addImage(image: Image) {
|
|
|
|
this.children.push(image);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
addDeleteText(text: string) {
|
|
|
|
this.children.push(new DeleteText(text));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addTab() {
|
|
|
|
this.children.push(new Tab());
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-10-15 12:30:05 +03:00
|
|
|
addPositionalTab(ptab: PositionalTab) {
|
|
|
|
this.children.push(ptab);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
addBreak(type: BreakType) {
|
|
|
|
this.children.push(new Break(type));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-03-16 05:54:17 +02:00
|
|
|
style(style: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.style(style);
|
2022-03-16 05:54:17 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
size(size: number) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.size(size);
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
color(color: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.color(color);
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
highlight(color: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.highlight(color);
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-06-07 18:10:44 +03:00
|
|
|
vertAlign(vertAlign: VertAlignType) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.vertAlign(vertAlign);
|
2021-06-07 18:10:44 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
bold() {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.bold();
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:47:29 +02:00
|
|
|
strike() {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.strike();
|
2021-12-21 08:47:29 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
italic() {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.italic();
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
underline(type: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.underline(type);
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
vanish() {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.vanish();
|
2020-01-30 16:14:25 +02:00
|
|
|
return this;
|
|
|
|
}
|
2020-06-08 07:41:13 +03:00
|
|
|
|
|
|
|
fonts(fonts: RunFonts) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.fonts(fonts);
|
2020-06-08 07:41:13 +03:00
|
|
|
return this;
|
|
|
|
}
|
2020-10-14 04:16:13 +03:00
|
|
|
|
2023-06-23 13:09:27 +03:00
|
|
|
spacing(characterSpacing: number) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.spacing(characterSpacing);
|
2020-10-14 04:16:13 +03:00
|
|
|
return this;
|
|
|
|
}
|
2021-03-20 17:16:43 +02:00
|
|
|
|
2022-01-17 08:27:24 +02:00
|
|
|
delete(author: string, date: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.delete(author, date);
|
2022-01-17 08:27:24 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
insert(author: string, date: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.delete(author, date);
|
2022-01-17 08:27:24 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-03-20 17:16:43 +02:00
|
|
|
textBorder(type: BorderType, size: number, space: number, color: string) {
|
2024-11-22 03:58:49 +02:00
|
|
|
this.property ??= createDefaultRunProperty();
|
|
|
|
this.property.textBorder(type, size, space, color);
|
2021-03-20 17:16:43 +02:00
|
|
|
return this;
|
|
|
|
}
|
2022-12-13 06:33:48 +02:00
|
|
|
|
|
|
|
build() {
|
|
|
|
let run = wasm.createRun();
|
|
|
|
this.children.forEach((child) => {
|
|
|
|
if (child instanceof Text) {
|
|
|
|
run = run.add_text(child.text);
|
|
|
|
} else if (child instanceof DeleteText) {
|
|
|
|
run = run.add_delete_text(child.text);
|
|
|
|
} else if (child instanceof Tab) {
|
|
|
|
run = run.add_tab();
|
2024-10-15 12:30:05 +03:00
|
|
|
} else if (child instanceof PositionalTab) {
|
|
|
|
run = run.add_ptab(child.buildWasmObject());
|
2022-12-13 06:33:48 +02:00
|
|
|
} else if (child instanceof Break) {
|
|
|
|
if (child.type === "column") {
|
|
|
|
run = run.add_break(wasm.BreakType.Column);
|
|
|
|
} else if (child.type === "page") {
|
|
|
|
run = run.add_break(wasm.BreakType.Page);
|
|
|
|
} else if (child.type === "textWrapping") {
|
|
|
|
run = run.add_break(wasm.BreakType.TextWrapping);
|
|
|
|
}
|
|
|
|
} else if (child instanceof Image) {
|
|
|
|
let pic = wasm.createPic(child.data);
|
|
|
|
if (child.w != null && child.h != null) {
|
|
|
|
pic = pic.size(child.w, child.h);
|
|
|
|
}
|
|
|
|
if (child._floating) {
|
|
|
|
pic = pic.floating();
|
|
|
|
}
|
|
|
|
if (child._offsetX != null) {
|
|
|
|
pic = pic.offset_x(child._offsetX);
|
|
|
|
}
|
|
|
|
if (child._offsetY != null) {
|
|
|
|
pic = pic.offset_x(child._offsetY);
|
|
|
|
}
|
|
|
|
if (child.rot != null) {
|
|
|
|
pic = pic.rotate(child.rot);
|
|
|
|
}
|
|
|
|
run = run.add_image(pic);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-22 03:58:49 +02:00
|
|
|
if (this.property) {
|
|
|
|
run = setRunProperty(run, this.property) as wasm.Run;
|
|
|
|
}
|
2022-12-13 06:33:48 +02:00
|
|
|
|
|
|
|
return run;
|
|
|
|
}
|
2020-01-30 16:14:25 +02:00
|
|
|
}
|