cvdl-ts 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/AnyLayout.js CHANGED
@@ -60,7 +60,7 @@ class FontDict {
60
60
  }
61
61
  console.log(`Source ${font.source}`);
62
62
  const font_data = await storage.load_font(font);
63
- console.log(font_data);
63
+ console.error(font_data);
64
64
  const fontkit_font = fontkit.create(font_data);
65
65
  this.fonts.set(font.full_name(), fontkit_font);
66
66
  }
@@ -99,6 +99,7 @@ async function render({ resume, layout_schemas, data_schemas, resume_layout, sto
99
99
  console.info(`Font loading time: ${end_time - start_time}ms for section ${section.section_name}`);
100
100
  // 2. Find the data schema for the section
101
101
  const _data_schema = data_schemas.find(s => s.schema_name === section.data_schema);
102
+ console.error(data_schemas);
102
103
  if (_data_schema === undefined) {
103
104
  throw new Error(`Could not find data schema ${section.data_schema}`);
104
105
  }
@@ -135,7 +136,7 @@ async function render({ resume, layout_schemas, data_schemas, resume_layout, sto
135
136
  const result = layout_schema
136
137
  .item_layout_schema
137
138
  .copy()
138
- .instantiate(item)
139
+ .instantiate(item.fields)
139
140
  .normalize(column_width, font_dict)
140
141
  .compute_boxes(font_dict);
141
142
  boxes.push(result);
package/dist/Font.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare class Font {
7
7
  source: FontSource;
8
8
  constructor(name: string, size: number, weight: FontWeight, style: FontStyle, source: FontSource);
9
9
  static fromJson(json: unknown): Font;
10
+ toJson(): unknown;
10
11
  static default_(): Font;
11
12
  full_name(): string;
12
13
  get_width(text: string, fonts: FontDict): number;
package/dist/Font.js CHANGED
@@ -13,15 +13,24 @@ class Font {
13
13
  if (typeof json !== "object" || json === null) {
14
14
  return Font.default_();
15
15
  }
16
- const name = ("name" in json) ? json.name : "Arial";
16
+ const name = ("name" in json) ? json.name : "Exo";
17
17
  const size = ("size" in json) ? json.size : 12;
18
18
  const weight = ("weight" in json) ? json.weight : "Medium";
19
19
  const style = ("style" in json) ? json.style : "Normal";
20
20
  const source = ("source" in json) ? json.source : "System";
21
21
  return new Font(name, size, weight, style, source);
22
22
  }
23
+ toJson() {
24
+ return {
25
+ name: this.name,
26
+ size: this.size,
27
+ weight: this.weight,
28
+ style: this.style,
29
+ source: this.source
30
+ };
31
+ }
23
32
  static default_() {
24
- return new Font("Arial", 12, "Medium", "Normal", "System");
33
+ return new Font("Exo", 12, "Medium", "Normal", "System");
25
34
  }
26
35
  full_name() {
27
36
  return this.name + "-" + this.weight + (this.style === "Italic" ? "Italic" : "");
package/dist/Layout.d.ts CHANGED
@@ -13,11 +13,14 @@ export declare class SectionLayout {
13
13
  constructor(inner: Stack | Row | Elem);
14
14
  copy(): SectionLayout;
15
15
  static constrMap(tag: string): Stack | Row | Elem;
16
+ static empty(): SectionLayout;
16
17
  static fromJson(json: any): SectionLayout;
18
+ toJson(): any;
17
19
  width(): Width;
18
20
  is_container(): boolean;
19
21
  is_ref(): boolean;
20
22
  type_(): "Stack" | "Row" | "Elem";
23
+ tag_(): "Stack" | "FlexRow" | "FrozenRow" | "Ref" | "Text";
21
24
  fonts(): Font[];
22
25
  with_margin(margin: Margin): SectionLayout;
23
26
  with_alignment(alignment: Alignment): SectionLayout;
package/dist/Layout.js CHANGED
@@ -34,6 +34,9 @@ class SectionLayout {
34
34
  }
35
35
  }
36
36
  }
37
+ static empty() {
38
+ return new SectionLayout(Stack.default_());
39
+ }
37
40
  static fromJson(json) {
38
41
  const key = Object.keys(json)[0];
39
42
  switch (key) {
@@ -60,7 +63,37 @@ class SectionLayout {
60
63
  return new SectionLayout(inner);
61
64
  }
62
65
  }
63
- throw new Error("Invalid layout");
66
+ throw new Error(`Invalid layout ${key}`);
67
+ }
68
+ toJson() {
69
+ switch (this.type_()) {
70
+ case "Stack":
71
+ case "Row": {
72
+ const container = this.inner;
73
+ return {
74
+ [this.tag_()]: {
75
+ elements: container.elements.map(e => e.toJson()),
76
+ margin: container.margin.toJson(),
77
+ alignment: container.alignment,
78
+ width: Width_1.Width.toJson(container.width),
79
+ },
80
+ };
81
+ }
82
+ case "Elem": {
83
+ const elem = this.inner;
84
+ return {
85
+ [this.tag_()]: {
86
+ item: elem.item,
87
+ margin: elem.margin.toJson(),
88
+ alignment: elem.alignment,
89
+ width: Width_1.Width.toJson(elem.width),
90
+ text_width: Width_1.Width.toJson(elem.text_width),
91
+ font: elem.font.toJson(),
92
+ url: elem.url,
93
+ },
94
+ };
95
+ }
96
+ }
64
97
  }
65
98
  width() {
66
99
  return this.inner.width;
@@ -74,6 +107,16 @@ class SectionLayout {
74
107
  type_() {
75
108
  return this.inner.tag;
76
109
  }
110
+ tag_() {
111
+ switch (this.type_()) {
112
+ case "Stack":
113
+ return "Stack";
114
+ case "Row":
115
+ return this.inner.is_frozen ? "FrozenRow" : "FlexRow";
116
+ case "Elem":
117
+ return this.is_ref() ? "Ref" : "Text";
118
+ }
119
+ }
77
120
  fonts() {
78
121
  switch (this.type_()) {
79
122
  case "Stack":
@@ -263,6 +306,9 @@ class SectionLayout {
263
306
  if (elem.is_ref) {
264
307
  throw new Error("Cannot compute textbox positions of uninstantiated layout");
265
308
  }
309
+ if (elem.item === "") {
310
+ return top_left.y;
311
+ }
266
312
  const height = elem.font.get_height(font_dict);
267
313
  const width = Width_1.Width.get_fixed_unchecked(elem.text_width);
268
314
  switch (elem.alignment) {
@@ -1,9 +1,17 @@
1
1
  import { SectionLayout } from "./Layout";
2
2
  export declare class LayoutSchema {
3
3
  schema_name: string;
4
+ data_schema_name: string;
4
5
  header_layout_schema: SectionLayout;
5
6
  item_layout_schema: SectionLayout;
6
- constructor(schema_name: string, header_layout_schema: SectionLayout, item_layout_schema: SectionLayout);
7
+ constructor(schema_name: string, data_schema_name: string, header_layout_schema: SectionLayout, item_layout_schema: SectionLayout);
8
+ static empty(schema_name: string, data_schema_name: string): LayoutSchema;
7
9
  static fromJson(json: any): LayoutSchema;
8
10
  fonts(): import("./Font").Font[];
11
+ toJson(): {
12
+ schema_name: string;
13
+ data_schema_name: string;
14
+ header_layout_schema: any;
15
+ item_layout_schema: any;
16
+ };
9
17
  }
@@ -3,13 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LayoutSchema = void 0;
4
4
  const Layout_1 = require("./Layout");
5
5
  class LayoutSchema {
6
- constructor(schema_name, header_layout_schema, item_layout_schema) {
6
+ constructor(schema_name, data_schema_name, header_layout_schema, item_layout_schema) {
7
7
  this.schema_name = schema_name;
8
+ this.data_schema_name = data_schema_name;
8
9
  this.header_layout_schema = header_layout_schema;
9
10
  this.item_layout_schema = item_layout_schema;
10
11
  }
12
+ static empty(schema_name, data_schema_name) {
13
+ return new LayoutSchema(schema_name, data_schema_name, Layout_1.SectionLayout.empty(), Layout_1.SectionLayout.empty());
14
+ }
11
15
  static fromJson(json) {
12
- return new LayoutSchema(json.schema_name, Layout_1.SectionLayout.fromJson(json.header_layout_schema), Layout_1.SectionLayout.fromJson(json.item_layout_schema));
16
+ console.error(json);
17
+ return new LayoutSchema(json.schema_name, json.data_schema_name, Layout_1.SectionLayout.fromJson(json.header_layout_schema), Layout_1.SectionLayout.fromJson(json.item_layout_schema));
13
18
  }
14
19
  fonts() {
15
20
  return [
@@ -17,5 +22,13 @@ class LayoutSchema {
17
22
  ...this.item_layout_schema.fonts(),
18
23
  ];
19
24
  }
25
+ toJson() {
26
+ return {
27
+ schema_name: this.schema_name,
28
+ data_schema_name: this.data_schema_name,
29
+ header_layout_schema: this.header_layout_schema.toJson(),
30
+ item_layout_schema: this.item_layout_schema.toJson(),
31
+ };
32
+ }
20
33
  }
21
34
  exports.LayoutSchema = LayoutSchema;
@@ -4,20 +4,19 @@ import { Font } from "./Font";
4
4
  import { LayoutSchema } from "./LayoutSchema";
5
5
  import { Resume } from "./Resume";
6
6
  import { ResumeLayout } from "./ResumeLayout";
7
- import { Storage } from "./Storage";
8
- export declare class LocalStorage implements Storage {
7
+ export declare class LocalStorage {
9
8
  initiate_storage(): Promise<void>;
10
- list_resumes(): Promise<string[]>;
11
- list_data_schemas(): Promise<string[]>;
12
- list_layout_schemas(): Promise<string[]>;
13
- list_resume_layouts(): Promise<string[]>;
14
- load_resume(resume_name: string): Promise<Resume>;
15
- load_data_schema(schema_name: string): Promise<DataSchema>;
16
- load_layout_schema(schema_name: string): Promise<LayoutSchema>;
17
- load_resume_layout(schema_name: string): Promise<ResumeLayout>;
18
- save_resume(resume_name: string, resume_data: Resume): Promise<void>;
9
+ list_resumes(): string[];
10
+ list_data_schemas(): string[];
11
+ list_layout_schemas(): string[];
12
+ list_resume_layouts(): string[];
13
+ load_resume(resume_name: string): Resume;
14
+ load_data_schema(schema_name: string): DataSchema;
15
+ load_layout_schema(schema_name: string): LayoutSchema;
16
+ load_resume_layout(schema_name: string): ResumeLayout;
17
+ save_resume(resume_name: string, resume_data: Resume): void;
19
18
  save_data_schema(data_schema: DataSchema): Promise<void>;
20
- save_layout_schema(layout_schema: LayoutSchema): Promise<void>;
21
- save_resume_layout(resume_layout: ResumeLayout): Promise<void>;
19
+ save_layout_schema(layout_schema: LayoutSchema): void;
20
+ save_resume_layout(resume_layout: ResumeLayout): void;
22
21
  load_font(font: Font): Promise<Buffer>;
23
22
  }
@@ -5,12 +5,13 @@ const DataSchema_1 = require("./DataSchema");
5
5
  const LayoutSchema_1 = require("./LayoutSchema");
6
6
  const Resume_1 = require("./Resume");
7
7
  const ResumeLayout_1 = require("./ResumeLayout");
8
+ // import { Storage } from "./Storage";
8
9
  class LocalStorage {
9
10
  async initiate_storage() {
10
11
  if (!localStorage.getItem("resumes")) {
11
- fetch("https://d2bnplhbawocbk.cloudfront.net/data/resumes/resume2.json").then((response) => {
12
+ fetch("https://d2bnplhbawocbk.cloudfront.net/data/resumes/resume5.json").then((response) => {
12
13
  response.json().then((resume) => {
13
- localStorage.setItem("resumes", JSON.stringify([{ name: "resume2", data: resume }]));
14
+ localStorage.setItem("resumes", JSON.stringify([{ name: "resume5", data: resume }]));
14
15
  });
15
16
  });
16
17
  }
@@ -22,7 +23,7 @@ class LocalStorage {
22
23
  });
23
24
  }
24
25
  if (!localStorage.getItem("section_layouts")) {
25
- fetch("https://d2bnplhbawocbk.cloudfront.net/data/layout-schemas.json").then((response) => {
26
+ fetch("https://d2bnplhbawocbk.cloudfront.net/data/layout-schemas2.json").then((response) => {
26
27
  response.json().then((section_layouts) => {
27
28
  localStorage.setItem("section_layouts", JSON.stringify(section_layouts));
28
29
  });
@@ -38,19 +39,19 @@ class LocalStorage {
38
39
  }
39
40
  list_resumes() {
40
41
  const resumes = JSON.parse(localStorage.getItem("resumes") || "[]").map((resume) => resume.name);
41
- return Promise.resolve(resumes);
42
+ return resumes;
42
43
  }
43
44
  list_data_schemas() {
44
45
  const schemas = JSON.parse(localStorage.getItem("data_schemas") || "[]").map((schema) => schema.schema_name);
45
- return Promise.resolve(schemas);
46
+ return schemas;
46
47
  }
47
48
  list_layout_schemas() {
48
49
  const schemas = JSON.parse(localStorage.getItem("section_layouts") || "[]").map((schema) => schema.schema_name);
49
- return Promise.resolve(schemas);
50
+ return schemas;
50
51
  }
51
52
  list_resume_layouts() {
52
53
  const schemas = JSON.parse(localStorage.getItem("resume_layouts") || "[]").map((schema) => schema.schema_name);
53
- return Promise.resolve(schemas);
54
+ return schemas;
54
55
  }
55
56
  load_resume(resume_name) {
56
57
  console.log(resume_name);
@@ -58,24 +59,22 @@ class LocalStorage {
58
59
  if (!resume) {
59
60
  throw new Error("Resume not found");
60
61
  }
61
- return Promise.resolve(Resume_1.Resume.fromJson(resume.data));
62
+ return Resume_1.Resume.fromJson(resume.data);
62
63
  }
63
64
  load_data_schema(schema_name) {
65
+ console.error(schema_name);
64
66
  const schema = JSON.parse(localStorage.getItem("data_schemas") || "[]").find((schema) => schema.schema_name === schema_name);
65
67
  if (!schema) {
66
68
  throw new Error("Data schema not found");
67
69
  }
68
- return Promise.resolve(DataSchema_1.DataSchema.fromJson(schema));
70
+ return DataSchema_1.DataSchema.fromJson(schema);
69
71
  }
70
72
  load_layout_schema(schema_name) {
71
73
  const schema = JSON.parse(localStorage.getItem("section_layouts") || "[]").find((schema) => schema.schema_name === schema_name);
72
- console.info(schema);
73
- console.info(schema_name);
74
- console.info(localStorage.getItem("section_layouts"));
75
74
  if (!schema) {
76
75
  throw new Error("Layout schema not found");
77
76
  }
78
- return Promise.resolve(LayoutSchema_1.LayoutSchema.fromJson(schema));
77
+ return LayoutSchema_1.LayoutSchema.fromJson(schema);
79
78
  }
80
79
  load_resume_layout(schema_name) {
81
80
  console.log(schema_name);
@@ -84,7 +83,7 @@ class LocalStorage {
84
83
  throw new Error("Resume layout not found");
85
84
  }
86
85
  console.info(schema);
87
- return Promise.resolve(ResumeLayout_1.ResumeLayout.fromJson(schema));
86
+ return ResumeLayout_1.ResumeLayout.fromJson(schema);
88
87
  }
89
88
  save_resume(resume_name, resume_data) {
90
89
  const resumes = JSON.parse(localStorage.getItem("resumes") || "[]");
@@ -96,18 +95,29 @@ class LocalStorage {
96
95
  resume.data = resume_data.toJson();
97
96
  }
98
97
  localStorage.setItem("resumes", JSON.stringify(resumes));
99
- return Promise.resolve();
100
98
  }
101
99
  save_data_schema(data_schema) {
102
100
  throw new Error("Method not implemented.");
103
101
  }
104
102
  save_layout_schema(layout_schema) {
105
- throw new Error("Method not implemented.");
103
+ const schemasDirectMapped = JSON.parse(localStorage.getItem("section_layouts") || "[]");
104
+ const schemas = schemasDirectMapped.map((schema) => LayoutSchema_1.LayoutSchema.fromJson(schema));
105
+ const schema = schemas.find((schema) => schema.schema_name === layout_schema.schema_name);
106
+ if (!schema) {
107
+ schemas.push(layout_schema);
108
+ }
109
+ else {
110
+ schema.header_layout_schema = layout_schema.header_layout_schema;
111
+ schema.item_layout_schema = layout_schema.item_layout_schema;
112
+ }
113
+ console.error(schemas);
114
+ localStorage.setItem("section_layouts", JSON.stringify(schemas.map((schema) => schema.toJson())));
106
115
  }
107
116
  save_resume_layout(resume_layout) {
108
117
  throw new Error("Method not implemented.");
109
118
  }
110
119
  async load_font(font) {
120
+ console.error(font);
111
121
  const path = `fonts/${font.full_name()}.ttf`;
112
122
  if (!localStorage.getItem(path)) {
113
123
  const response = await fetch(`https://d2bnplhbawocbk.cloudfront.net/data/${path}`);
package/dist/Margin.d.ts CHANGED
@@ -7,4 +7,10 @@ export declare class Margin {
7
7
  copy(): Margin;
8
8
  static default_(): Margin;
9
9
  static fromJson(json: any): Margin;
10
+ toJson(): {
11
+ top: number;
12
+ bottom: number;
13
+ left: number;
14
+ right: number;
15
+ };
10
16
  }
package/dist/Margin.js CHANGED
@@ -22,5 +22,13 @@ class Margin {
22
22
  static fromJson(json) {
23
23
  return new Margin(json.top, json.bottom, json.left, json.right);
24
24
  }
25
+ toJson() {
26
+ return {
27
+ top: this.top,
28
+ bottom: this.bottom,
29
+ left: this.left,
30
+ right: this.right,
31
+ };
32
+ }
25
33
  }
26
34
  exports.Margin = Margin;
package/dist/Resume.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare class ResumeSection {
17
17
  data_schema: string;
18
18
  layout_schema: string;
19
19
  data: Map<ItemName, ItemContent>;
20
- items: Map<ItemName, ItemContent>[];
20
+ items: Item[];
21
21
  constructor();
22
22
  toJson(): unknown;
23
23
  static fromJson(json: unknown): ResumeSection;
@@ -38,6 +38,14 @@ export type ItemContent = {
38
38
  text: string;
39
39
  };
40
40
  };
41
+ export type Item = {
42
+ id: string;
43
+ fields: Map<ItemName, ItemContent>;
44
+ };
45
+ export declare namespace Item {
46
+ function fromJson(json: unknown): Item;
47
+ function toJson(item: Item): unknown;
48
+ }
41
49
  export declare namespace ItemContent {
42
50
  function fromJson(json: unknown): ItemContent;
43
51
  function None(): ItemContent;
package/dist/Resume.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-namespace */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ItemContent = exports.ResumeSection = exports.Resume = void 0;
4
+ exports.ItemContent = exports.Item = exports.ResumeSection = exports.Resume = void 0;
5
5
  class Resume {
6
6
  constructor(layout, sections) {
7
7
  this.layout = layout;
@@ -35,6 +35,8 @@ class Resume {
35
35
  }
36
36
  }
37
37
  data_schemas() {
38
+ console.error(this.sections);
39
+ console.error(this.sections.map(section => section.data_schema));
38
40
  return this.sections.map(section => section.data_schema);
39
41
  }
40
42
  layout_schemas() {
@@ -65,7 +67,7 @@ class ResumeSection {
65
67
  data_schema: this.data_schema,
66
68
  layout_schema: this.layout_schema,
67
69
  data: Object.fromEntries(this.data),
68
- items: this.items.map(item => Object.fromEntries(item)),
70
+ items: this.items.map(item => Item.toJson(item)),
69
71
  };
70
72
  }
71
73
  static fromJson(json) {
@@ -86,13 +88,43 @@ class ResumeSection {
86
88
  // @ts-ignore
87
89
  section.data = new Map([...data].map(([key, value]) => [key, ItemContent.fromJson(value)]));
88
90
  section.items = json.items.map(item => {
89
- const data = new Map(Object.entries(item));
90
- return new Map([...data].map(([key, value]) => [key, ItemContent.fromJson(value)]));
91
+ const data = new Map(Object.entries(item.fields));
92
+ return {
93
+ id: item.id,
94
+ fields: new Map([...data].map(([key, value]) => [key, ItemContent.fromJson(value)]))
95
+ };
91
96
  });
92
97
  return section;
93
98
  }
94
99
  }
95
100
  exports.ResumeSection = ResumeSection;
101
+ var Item;
102
+ (function (Item) {
103
+ function fromJson(json) {
104
+ if (typeof json !== "object") {
105
+ throw new Error("Item must be an object");
106
+ }
107
+ if (json === null) {
108
+ throw new Error("Item must not be null");
109
+ }
110
+ if (!("id" in json) || !("fields" in json)) {
111
+ throw new Error("Item must have an id and fields");
112
+ }
113
+ const item = {
114
+ id: json.id,
115
+ fields: new Map(Object.entries(json.fields))
116
+ };
117
+ return item;
118
+ }
119
+ Item.fromJson = fromJson;
120
+ function toJson(item) {
121
+ return {
122
+ id: item.id,
123
+ fields: Object.fromEntries(item.fields)
124
+ };
125
+ }
126
+ Item.toJson = toJson;
127
+ })(Item || (exports.Item = Item = {}));
96
128
  var ItemContent;
97
129
  (function (ItemContent) {
98
130
  // @ts-ignore
@@ -106,10 +138,11 @@ var ItemContent;
106
138
  if (Array.isArray(json)) {
107
139
  return { tag: "List", value: json.map(fromJson) };
108
140
  }
141
+ if (typeof json === "object" && ("tag" in json) && json.tag === "None") {
142
+ return { tag: "None" };
143
+ }
109
144
  if (typeof json === "object" && ("tag" in json) && ("value" in json)) {
110
145
  switch (json.tag) {
111
- case "None":
112
- return { tag: "None" };
113
146
  case "String":
114
147
  return { tag: "String", value: json.value };
115
148
  case "List":
@@ -121,7 +154,7 @@ var ItemContent;
121
154
  else if (typeof json === "object" && ("url" in json) && ("text" in json)) {
122
155
  return { tag: "Url", value: { url: json.url, text: json.text } };
123
156
  }
124
- throw new Error("ItemContent must be a string, an array, or an object");
157
+ throw new Error(`Invalid ItemContent(${JSON.stringify(json)}): ItemContent must be a string, an array, or an object`);
125
158
  }
126
159
  ItemContent.fromJson = fromJson;
127
160
  function None() {
package/dist/Width.d.ts CHANGED
@@ -20,4 +20,5 @@ export declare namespace Width {
20
20
  function get_fixed_unchecked(self: Width): number;
21
21
  function scale(self: Width, scale: number): Width;
22
22
  function fromJson(json: any): Width;
23
+ function toJson(self: Width): any;
23
24
  }
package/dist/Width.js CHANGED
@@ -76,4 +76,15 @@ var Width;
76
76
  }
77
77
  }
78
78
  Width.fromJson = fromJson;
79
+ function toJson(self) {
80
+ switch (self.tag) {
81
+ case "Percent":
82
+ return self.value + "%";
83
+ case "Absolute":
84
+ return self.value + "px";
85
+ case "Fill":
86
+ return "Fill";
87
+ }
88
+ }
89
+ Width.toJson = toJson;
79
90
  })(Width || (exports.Width = Width = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvdl-ts",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Typescript Implementation of CVDL Compiler",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",