@trebco/treb 23.6.5 → 25.0.0-rc1

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.
Files changed (217) hide show
  1. package/.eslintignore +8 -0
  2. package/.eslintrc.js +164 -0
  3. package/README-shadow-DOM.md +88 -0
  4. package/README.md +37 -130
  5. package/api-config.json +29 -0
  6. package/api-generator/api-generator-types.ts +82 -0
  7. package/api-generator/api-generator.ts +1172 -0
  8. package/api-generator/package.json +3 -0
  9. package/build/treb-spreadsheet.mjs +14 -0
  10. package/{treb.d.ts → build/treb.d.ts} +285 -269
  11. package/esbuild-custom-element.mjs +336 -0
  12. package/esbuild.js +305 -0
  13. package/package.json +43 -14
  14. package/treb-base-types/package.json +5 -0
  15. package/treb-base-types/src/api_types.ts +36 -0
  16. package/treb-base-types/src/area.ts +583 -0
  17. package/treb-base-types/src/basic_types.ts +45 -0
  18. package/treb-base-types/src/cell.ts +612 -0
  19. package/treb-base-types/src/cells.ts +1066 -0
  20. package/treb-base-types/src/color.ts +124 -0
  21. package/treb-base-types/src/import.ts +71 -0
  22. package/treb-base-types/src/index-standalone.ts +29 -0
  23. package/treb-base-types/src/index.ts +42 -0
  24. package/treb-base-types/src/layout.ts +47 -0
  25. package/treb-base-types/src/localization.ts +187 -0
  26. package/treb-base-types/src/rectangle.ts +145 -0
  27. package/treb-base-types/src/render_text.ts +72 -0
  28. package/treb-base-types/src/style.ts +545 -0
  29. package/treb-base-types/src/table.ts +109 -0
  30. package/treb-base-types/src/text_part.ts +54 -0
  31. package/treb-base-types/src/theme.ts +608 -0
  32. package/treb-base-types/src/union.ts +152 -0
  33. package/treb-base-types/src/value-type.ts +164 -0
  34. package/treb-base-types/style/resizable.css +59 -0
  35. package/treb-calculator/modern.tsconfig.json +11 -0
  36. package/treb-calculator/package.json +5 -0
  37. package/treb-calculator/src/calculator.ts +2546 -0
  38. package/treb-calculator/src/complex-math.ts +558 -0
  39. package/treb-calculator/src/dag/array-vertex.ts +198 -0
  40. package/treb-calculator/src/dag/graph.ts +951 -0
  41. package/treb-calculator/src/dag/leaf_vertex.ts +118 -0
  42. package/treb-calculator/src/dag/spreadsheet_vertex.ts +327 -0
  43. package/treb-calculator/src/dag/spreadsheet_vertex_base.ts +44 -0
  44. package/treb-calculator/src/dag/vertex.ts +352 -0
  45. package/treb-calculator/src/descriptors.ts +162 -0
  46. package/treb-calculator/src/expression-calculator.ts +1069 -0
  47. package/treb-calculator/src/function-error.ts +103 -0
  48. package/treb-calculator/src/function-library.ts +103 -0
  49. package/treb-calculator/src/functions/base-functions.ts +1214 -0
  50. package/treb-calculator/src/functions/checkbox.ts +164 -0
  51. package/treb-calculator/src/functions/complex-functions.ts +253 -0
  52. package/treb-calculator/src/functions/finance-functions.ts +399 -0
  53. package/treb-calculator/src/functions/information-functions.ts +102 -0
  54. package/treb-calculator/src/functions/matrix-functions.ts +182 -0
  55. package/treb-calculator/src/functions/sparkline.ts +335 -0
  56. package/treb-calculator/src/functions/statistics-functions.ts +350 -0
  57. package/treb-calculator/src/functions/text-functions.ts +298 -0
  58. package/treb-calculator/src/index.ts +27 -0
  59. package/treb-calculator/src/notifier-types.ts +59 -0
  60. package/treb-calculator/src/primitives.ts +428 -0
  61. package/treb-calculator/src/utilities.ts +305 -0
  62. package/treb-charts/package.json +5 -0
  63. package/treb-charts/src/chart-functions.ts +156 -0
  64. package/treb-charts/src/chart-types.ts +230 -0
  65. package/treb-charts/src/chart.ts +1288 -0
  66. package/treb-charts/src/index.ts +24 -0
  67. package/treb-charts/src/main.ts +37 -0
  68. package/treb-charts/src/rectangle.ts +52 -0
  69. package/treb-charts/src/renderer.ts +1841 -0
  70. package/treb-charts/src/util.ts +122 -0
  71. package/treb-charts/style/charts.scss +221 -0
  72. package/treb-charts/style/old-charts.scss +250 -0
  73. package/treb-embed/markup/layout.html +137 -0
  74. package/treb-embed/markup/toolbar.html +175 -0
  75. package/treb-embed/modern.tsconfig.json +25 -0
  76. package/treb-embed/src/custom-element/content-types.d.ts +18 -0
  77. package/treb-embed/src/custom-element/global.d.ts +11 -0
  78. package/treb-embed/src/custom-element/spreadsheet-constructor.ts +1227 -0
  79. package/treb-embed/src/custom-element/treb-global.ts +44 -0
  80. package/treb-embed/src/custom-element/treb-spreadsheet-element.ts +52 -0
  81. package/treb-embed/src/embedded-spreadsheet.ts +5362 -0
  82. package/treb-embed/src/index.ts +16 -0
  83. package/treb-embed/src/language-model.ts +41 -0
  84. package/treb-embed/src/options.ts +320 -0
  85. package/treb-embed/src/progress-dialog.ts +228 -0
  86. package/treb-embed/src/selection-state.ts +16 -0
  87. package/treb-embed/src/spinner.ts +42 -0
  88. package/treb-embed/src/toolbar-message.ts +96 -0
  89. package/treb-embed/src/types.ts +167 -0
  90. package/treb-embed/style/autocomplete.scss +103 -0
  91. package/treb-embed/style/dark-theme.scss +114 -0
  92. package/treb-embed/style/defaults.scss +36 -0
  93. package/treb-embed/style/dialog.scss +181 -0
  94. package/treb-embed/style/dropdown-select.scss +101 -0
  95. package/treb-embed/style/formula-bar.scss +193 -0
  96. package/treb-embed/style/grid.scss +374 -0
  97. package/treb-embed/style/layout.scss +424 -0
  98. package/treb-embed/style/mouse-mask.scss +67 -0
  99. package/treb-embed/style/note.scss +92 -0
  100. package/treb-embed/style/overlay-editor.scss +102 -0
  101. package/treb-embed/style/spinner.scss +92 -0
  102. package/treb-embed/style/tab-bar.scss +228 -0
  103. package/treb-embed/style/table.scss +80 -0
  104. package/treb-embed/style/theme-defaults.scss +444 -0
  105. package/treb-embed/style/toolbar.scss +416 -0
  106. package/treb-embed/style/tooltip.scss +68 -0
  107. package/treb-embed/style/treb-icons.scss +130 -0
  108. package/treb-embed/style/treb-spreadsheet-element.scss +20 -0
  109. package/treb-embed/style/z-index.scss +43 -0
  110. package/treb-export/docs/charts.md +68 -0
  111. package/treb-export/modern.tsconfig.json +19 -0
  112. package/treb-export/package.json +4 -0
  113. package/treb-export/src/address-type.ts +77 -0
  114. package/treb-export/src/base-template.ts +22 -0
  115. package/treb-export/src/column-width.ts +85 -0
  116. package/treb-export/src/drawing2/chart-template-components2.ts +389 -0
  117. package/treb-export/src/drawing2/chart2.ts +282 -0
  118. package/treb-export/src/drawing2/column-chart-template2.ts +521 -0
  119. package/treb-export/src/drawing2/donut-chart-template2.ts +296 -0
  120. package/treb-export/src/drawing2/drawing2.ts +355 -0
  121. package/treb-export/src/drawing2/embedded-image.ts +71 -0
  122. package/treb-export/src/drawing2/scatter-chart-template2.ts +555 -0
  123. package/treb-export/src/export-worker/export-worker.ts +99 -0
  124. package/treb-export/src/export-worker/index-modern.ts +22 -0
  125. package/treb-export/src/export2.ts +2204 -0
  126. package/treb-export/src/import2.ts +882 -0
  127. package/treb-export/src/relationship.ts +36 -0
  128. package/treb-export/src/shared-strings2.ts +128 -0
  129. package/treb-export/src/template-2.ts +22 -0
  130. package/treb-export/src/unescape_xml.ts +47 -0
  131. package/treb-export/src/workbook-sheet2.ts +182 -0
  132. package/treb-export/src/workbook-style2.ts +1285 -0
  133. package/treb-export/src/workbook-theme2.ts +88 -0
  134. package/treb-export/src/workbook2.ts +491 -0
  135. package/treb-export/src/xml-utils.ts +201 -0
  136. package/treb-export/template/base/[Content_Types].xml +2 -0
  137. package/treb-export/template/base/_rels/.rels +2 -0
  138. package/treb-export/template/base/docProps/app.xml +2 -0
  139. package/treb-export/template/base/docProps/core.xml +12 -0
  140. package/treb-export/template/base/xl/_rels/workbook.xml.rels +2 -0
  141. package/treb-export/template/base/xl/sharedStrings.xml +2 -0
  142. package/treb-export/template/base/xl/styles.xml +2 -0
  143. package/treb-export/template/base/xl/theme/theme1.xml +2 -0
  144. package/treb-export/template/base/xl/workbook.xml +2 -0
  145. package/treb-export/template/base/xl/worksheets/sheet1.xml +2 -0
  146. package/treb-export/template/base.xlsx +0 -0
  147. package/treb-format/package.json +8 -0
  148. package/treb-format/src/format.test.ts +213 -0
  149. package/treb-format/src/format.ts +942 -0
  150. package/treb-format/src/format_cache.ts +199 -0
  151. package/treb-format/src/format_parser.ts +723 -0
  152. package/treb-format/src/index.ts +25 -0
  153. package/treb-format/src/number_format_section.ts +100 -0
  154. package/treb-format/src/value_parser.ts +337 -0
  155. package/treb-grid/package.json +5 -0
  156. package/treb-grid/src/editors/autocomplete.ts +394 -0
  157. package/treb-grid/src/editors/autocomplete_matcher.ts +260 -0
  158. package/treb-grid/src/editors/formula_bar.ts +473 -0
  159. package/treb-grid/src/editors/formula_editor_base.ts +910 -0
  160. package/treb-grid/src/editors/overlay_editor.ts +511 -0
  161. package/treb-grid/src/index.ts +37 -0
  162. package/treb-grid/src/layout/base_layout.ts +2618 -0
  163. package/treb-grid/src/layout/grid_layout.ts +299 -0
  164. package/treb-grid/src/layout/rectangle_cache.ts +86 -0
  165. package/treb-grid/src/render/selection-renderer.ts +414 -0
  166. package/treb-grid/src/render/svg_header_overlay.ts +93 -0
  167. package/treb-grid/src/render/svg_selection_block.ts +187 -0
  168. package/treb-grid/src/render/tile_renderer.ts +2122 -0
  169. package/treb-grid/src/types/annotation.ts +216 -0
  170. package/treb-grid/src/types/border_constants.ts +34 -0
  171. package/treb-grid/src/types/clipboard_data.ts +31 -0
  172. package/treb-grid/src/types/data_model.ts +334 -0
  173. package/treb-grid/src/types/drag_mask.ts +81 -0
  174. package/treb-grid/src/types/grid.ts +7743 -0
  175. package/treb-grid/src/types/grid_base.ts +3644 -0
  176. package/treb-grid/src/types/grid_command.ts +470 -0
  177. package/treb-grid/src/types/grid_events.ts +124 -0
  178. package/treb-grid/src/types/grid_options.ts +97 -0
  179. package/treb-grid/src/types/grid_selection.ts +60 -0
  180. package/treb-grid/src/types/named_range.ts +369 -0
  181. package/treb-grid/src/types/scale-control.ts +202 -0
  182. package/treb-grid/src/types/serialize_options.ts +72 -0
  183. package/treb-grid/src/types/set_range_options.ts +52 -0
  184. package/treb-grid/src/types/sheet.ts +3099 -0
  185. package/treb-grid/src/types/sheet_types.ts +95 -0
  186. package/treb-grid/src/types/tab_bar.ts +464 -0
  187. package/treb-grid/src/types/tile.ts +59 -0
  188. package/treb-grid/src/types/update_flags.ts +75 -0
  189. package/treb-grid/src/util/dom_utilities.ts +44 -0
  190. package/treb-grid/src/util/fontmetrics2.ts +179 -0
  191. package/treb-grid/src/util/ua.ts +104 -0
  192. package/treb-logo.svg +18 -0
  193. package/treb-parser/package.json +5 -0
  194. package/treb-parser/src/csv-parser.ts +122 -0
  195. package/treb-parser/src/index.ts +25 -0
  196. package/treb-parser/src/md-parser.ts +526 -0
  197. package/treb-parser/src/parser-types.ts +397 -0
  198. package/treb-parser/src/parser.test.ts +298 -0
  199. package/treb-parser/src/parser.ts +2673 -0
  200. package/treb-utils/package.json +5 -0
  201. package/treb-utils/src/dispatch.ts +57 -0
  202. package/treb-utils/src/event_source.ts +147 -0
  203. package/treb-utils/src/ievent_source.ts +33 -0
  204. package/treb-utils/src/index.ts +31 -0
  205. package/treb-utils/src/measurement.ts +174 -0
  206. package/treb-utils/src/resizable.ts +160 -0
  207. package/treb-utils/src/scale.ts +137 -0
  208. package/treb-utils/src/serialize_html.ts +124 -0
  209. package/treb-utils/src/template.ts +70 -0
  210. package/treb-utils/src/validate_uri.ts +61 -0
  211. package/tsconfig.json +10 -0
  212. package/tsproject.json +30 -0
  213. package/util/license-plugin-esbuild.js +86 -0
  214. package/util/list-css-vars.sh +46 -0
  215. package/README-esm.md +0 -37
  216. package/treb-bundle.css +0 -2
  217. package/treb-bundle.mjs +0 -15
@@ -0,0 +1,124 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ /**
23
+ * utility functions, primarily for adjusting lightness. since we generally
24
+ * traffic in RGB (or symbolic colors) that requires translating to/from HSL.
25
+ */
26
+ class ColorFunctions {
27
+
28
+ /** darken rgb color by amount (%), optionally relative */
29
+ public Darken(r: number, g: number, b: number, amount: number, relative = false) {
30
+
31
+ // eslint-disable-next-line prefer-const
32
+ let { h, s, l } = this.RGBToHSL(r, g, b);
33
+ if (relative) l -= l * amount / 100;
34
+ else l -= amount / 100;
35
+ l = Math.max(0, Math.min(1, l));
36
+ return this.HSLToRGB(h, s, l);
37
+ }
38
+
39
+ /** lighten rgb color by amount (%), optionally relative */
40
+ public Lighten(r: number, g: number, b: number, amount: number, relative = false) {
41
+
42
+ // eslint-disable-next-line prefer-const
43
+ let { h, s, l } = this.RGBToHSL(r, g, b);
44
+ if (relative) l += l * amount / 100;
45
+ else l += amount / 100;
46
+ l = Math.max(0, Math.min(1, l));
47
+ return this.HSLToRGB(h, s, l);
48
+ }
49
+
50
+ public RGBToHSL(r: number, g: number, b: number) {
51
+
52
+ r /= 255;
53
+ g /= 255;
54
+ b /= 255;
55
+
56
+ const max = Math.max(r, g, b);
57
+ const min = Math.min(r, g, b);
58
+
59
+ let h = 0;
60
+ let s = 0;
61
+
62
+ const l = (max + min) / 2;
63
+
64
+ if (max === min) {
65
+ h = s = 0;
66
+ }
67
+ else {
68
+ const d = max - min;
69
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
70
+ switch (max) {
71
+ case r:
72
+ h = (g - b) / d + (g < b ? 6 : 0);
73
+ break;
74
+ case g:
75
+ h = (b - r) / d + 2;
76
+ break;
77
+ case b:
78
+ h = (r - g) / d + 4;
79
+ break;
80
+ }
81
+ h /= 6;
82
+ }
83
+
84
+ return { h: h * 360, s, l };
85
+ }
86
+
87
+ public HSLToRGB(h: number, s: number, l: number) {
88
+
89
+ let r: number;
90
+ let g: number;
91
+ let b: number;
92
+
93
+ if (s === 0) {
94
+ r = g = b = l;
95
+ }
96
+ else {
97
+ h = h / 360;
98
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
99
+ const p = 2 * l - q;
100
+ r = this.HueToRGB(p, q, h + 1 / 3);
101
+ g = this.HueToRGB(p, q, h);
102
+ b = this.HueToRGB(p, q, h - 1 / 3);
103
+ }
104
+
105
+ return {
106
+ r: Math.round(r * 255),
107
+ g: Math.round(g * 255),
108
+ b: Math.round(b * 255),
109
+ };
110
+ }
111
+
112
+ private HueToRGB(p: number, q: number, t: number) {
113
+ if (t < 0) t += 1;
114
+ if (t > 1) t -= 1;
115
+ if (t < 1 / 6) return p + (q - p) * 6 * t;
116
+ if (t < 1 / 2) return q;
117
+ if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
118
+ return p;
119
+ }
120
+
121
+ }
122
+
123
+ /** @internal */
124
+ export const Color = new ColorFunctions();
@@ -0,0 +1,71 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ import type { Style } from './style';
23
+ import type { ValueType } from './value-type';
24
+ import type { IArea } from './area';
25
+ import type { AnnotationLayout } from './layout';
26
+ import type { DataValidation } from './cell';
27
+ import type { Table } from './table';
28
+
29
+ export interface CellParseResult {
30
+ row: number,
31
+ column: number,
32
+ type: ValueType,
33
+ value: number|string|undefined|boolean,
34
+ calculated?: number|string|undefined|boolean,
35
+ calculated_type?: ValueType,
36
+ style_ref?: number,
37
+ hyperlink?: string,
38
+ validation?: DataValidation,
39
+ merge_area?: IArea,
40
+ area?: IArea,
41
+ table?: Table,
42
+ }
43
+
44
+ export interface AnchoredAnnotation {
45
+ layout: AnnotationLayout;
46
+ type?: string;
47
+ formula?: string;
48
+ }
49
+
50
+ /**
51
+ * this is moved from export to avoid a circular reference
52
+ */
53
+ export interface ImportedSheetData {
54
+ name: string|undefined;
55
+ cells: CellParseResult[];
56
+ default_column_width: number;
57
+ column_widths: number[];
58
+ row_heights: number[];
59
+ styles: Style.Properties[];
60
+
61
+ // optional, for backcompat
62
+ sheet_style?: number;
63
+ column_styles?: number[];
64
+
65
+ // new
66
+ annotations?: AnchoredAnnotation[];
67
+
68
+ hidden?: boolean;
69
+
70
+ }
71
+
@@ -0,0 +1,29 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ /**
23
+ * this is a special index for inclusion in the standalone format lib
24
+ */
25
+
26
+ export * from './localization';
27
+ export * from './text_part';
28
+ export * from './value-type';
29
+
@@ -0,0 +1,42 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ export * from './area';
23
+ export * from './cell';
24
+ export * from './cells';
25
+ export * from './localization';
26
+ export * from './basic_types';
27
+ export * from './rectangle';
28
+ export * from './style';
29
+ export * from './text_part';
30
+ export * from './import';
31
+ export * from './union';
32
+ export * from './value-type';
33
+ export * from './theme';
34
+ export * from './color';
35
+ export * from './layout';
36
+ export * from './render_text';
37
+ export * from './api_types';
38
+ export * from './table';
39
+
40
+ // import * as Style from './style';
41
+ // export { Style };
42
+
@@ -0,0 +1,47 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ import type { ICellAddress } from './area';
23
+
24
+ /**
25
+ * offset from corner, as % of cell
26
+ */
27
+ export interface AddressOffset {
28
+ x: number,
29
+ y: number,
30
+ }
31
+
32
+ /**
33
+ * represents one corner of a layout rectangle
34
+ */
35
+ export interface Corner {
36
+ address: ICellAddress;
37
+ offset: AddressOffset;
38
+ }
39
+
40
+ /**
41
+ * represents the layout of an annotation, reference to the sheet
42
+ */
43
+ export interface AnnotationLayout {
44
+ tl: Corner;
45
+ br: Corner;
46
+ }
47
+
@@ -0,0 +1,187 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ /**
23
+ * the point of localization is to have a central, single source of truth
24
+ * for the current locale. this can come from the browser, via a GET
25
+ * parameter (mostly for testing), or explicitly from a method call.
26
+ *
27
+ * based on locale we report settings for decimal separator, digit grouping,
28
+ * and argument separator.
29
+ */
30
+ export class Localization {
31
+
32
+ public static locale = 'en-us'; // default, for node
33
+
34
+ public static decimal_separator: ('.' | ',') = '.';
35
+ public static argument_separator: ','|';' = ',';
36
+ public static grouping_separator: ','|' ' = ',';
37
+
38
+ public static date_components = {
39
+ short_days: [
40
+ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
41
+ ],
42
+
43
+ long_days: [
44
+ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
45
+ ],
46
+
47
+ short_months: [
48
+ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
49
+ ],
50
+
51
+ long_months: [
52
+ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
53
+ 'September', 'October', 'November', 'December',
54
+ ],
55
+ };
56
+
57
+ /**
58
+ * update the locale. this will be called on module load (should
59
+ * be just once), but can be called subsequently to update.
60
+ *
61
+ * priority:
62
+ * (1) function argument
63
+ * (2) get parameter
64
+ * (3) navigator.languages[0]
65
+ * (4) navigator.language
66
+ *
67
+ * regarding languages[0] vs language, see
68
+ * https://stackoverflow.com/a/33204290
69
+ *
70
+ * @param locale explicitly set locale
71
+ */
72
+ public static UpdateLocale(locale?: string): void {
73
+
74
+ if (locale) {
75
+ this.locale = locale; // 1
76
+ }
77
+ else if (typeof self !== 'undefined') {
78
+
79
+ // const location = ((typeof self === 'undefined' || typeof self.document === 'undefined') ?
80
+ // undefined : self.document.location);
81
+
82
+ const location = self?.document?.location;
83
+
84
+ if (location && location.search &&
85
+ /locale=([^?&]+?)(?:\?|$|&)/.test(location.search)) {
86
+ const match = location.search.match(/locale=(.*?)(?:\?|$|&)/);
87
+ if (match) this.locale = match[1];
88
+ console.info('override locale', this.locale);
89
+ }
90
+ else if (typeof navigator !== 'undefined') {
91
+ if (navigator.languages && navigator.languages[0]) {
92
+ this.locale = navigator.languages[0];
93
+ }
94
+ else {
95
+ this.locale = navigator.language;
96
+ }
97
+ }
98
+
99
+ }
100
+
101
+ const decimal_separator = new Intl.NumberFormat(this.locale,
102
+ {minimumFractionDigits: 1}).format(3.3).replace(/\d/g, '');
103
+
104
+ this.decimal_separator = (decimal_separator === ',') ? ',' : '.';
105
+
106
+ if (this.decimal_separator === ',') {
107
+ this.argument_separator = ';';
108
+
109
+ // FIXME: should be half-space (char?)
110
+ //
111
+ // the appropriate character (I think) is \u2009, "thin space", but
112
+ // it seems to be rendered as full-width space in monospace fonts --
113
+ // which makes sense -- and since we mostly use those it's probably
114
+ // immaterial.
115
+ //
116
+ // it might be useful for fonts with variable-width characters but
117
+ // tabular numbers, in which case we would want to use it.
118
+ //
119
+ // FIXME: does format use this field, or does it have its own? (...)
120
+
121
+ this.grouping_separator = ' ';
122
+ }
123
+ else {
124
+ this.argument_separator = ',';
125
+ this.grouping_separator = ',';
126
+ }
127
+
128
+ // moved from number format lib
129
+
130
+
131
+ let date = new Date(2000, 0, 2, 12, 0, 0, 0); // Sun Jan 2
132
+ this.UpdateDateComponent(0, 0, date);
133
+
134
+ date = new Date(2000, 1, 7, 12, 0, 0, 0); // Mon Feb 7
135
+ this.UpdateDateComponent(1, 1, date);
136
+
137
+ date = new Date(2000, 2, 7, 12, 0, 0, 0); // Tue Mar 7
138
+ this.UpdateDateComponent(2, 2, date);
139
+
140
+ date = new Date(2000, 3, 5, 12, 0, 0, 0); // Wed Apr 5
141
+ this.UpdateDateComponent(3, 3, date);
142
+
143
+ date = new Date(2000, 4, 4, 12, 0, 0, 0); // Thur May 4
144
+ this.UpdateDateComponent(4, 4, date);
145
+
146
+ date = new Date(2000, 5, 2, 12, 0, 0, 0); // Fri Jun 2
147
+ this.UpdateDateComponent(5, 5, date);
148
+
149
+ date = new Date(2000, 6, 1, 12, 0, 0, 0); // Sat Jul 1
150
+ this.UpdateDateComponent(6, 6, date);
151
+
152
+ date = new Date(2000, 7, 1, 12, 0, 0, 0); // Aug
153
+ this.UpdateDateComponent(7, -1, date);
154
+
155
+ date = new Date(2000, 8, 1, 12, 0, 0, 0); // Sep
156
+ this.UpdateDateComponent(8, -1, date);
157
+
158
+ date = new Date(2000, 9, 1, 12, 0, 0, 0); // Oct
159
+ this.UpdateDateComponent(9, -1, date);
160
+
161
+ date = new Date(2000, 10, 1, 12, 0, 0, 0); // Nov
162
+ this.UpdateDateComponent(10, -1, date);
163
+
164
+ date = new Date(2000, 11, 1, 12, 0, 0, 0); // Dec
165
+ this.UpdateDateComponent(11, -1, date);
166
+
167
+ /*
168
+ console.info('LX', this.locale, this.date_components.short_days,
169
+ this.date_components.long_days, this.date_components.short_months,
170
+ this.date_components.long_months);
171
+ */
172
+ }
173
+
174
+ private static UpdateDateComponent(month_index: number, day_index: number, date: Date){
175
+ if (day_index >= 0) {
176
+ this.date_components.short_days[day_index] = date.toLocaleString(this.locale, {weekday: 'short'});
177
+ this.date_components.long_days[day_index] = date.toLocaleString(this.locale, {weekday: 'long'});
178
+ }
179
+ if (month_index >= 0) {
180
+ this.date_components.short_months[month_index] = date.toLocaleString(this.locale, {month: 'short'});
181
+ this.date_components.long_months[month_index] = date.toLocaleString(this.locale, {month: 'long'});
182
+ }
183
+ }
184
+
185
+ }
186
+
187
+ Localization.UpdateLocale(); // always call
@@ -0,0 +1,145 @@
1
+ /*
2
+ * This file is part of TREB.
3
+ *
4
+ * TREB is free software: you can redistribute it and/or modify it under the
5
+ * terms of the GNU General Public License as published by the Free Software
6
+ * Foundation, either version 3 of the License, or (at your option) any
7
+ * later version.
8
+ *
9
+ * TREB is distributed in the hope that it will be useful, but WITHOUT ANY
10
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
+ * details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License along
15
+ * with TREB. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ * Copyright 2022-2023 trebco, llc.
18
+ * info@treb.app
19
+ *
20
+ */
21
+
22
+ /** structure represents rectangle coordinates */
23
+ export interface IRectangle {
24
+ top: number;
25
+ left: number;
26
+ width: number;
27
+ height: number;
28
+ }
29
+
30
+ export class Rectangle implements IRectangle {
31
+
32
+ public get right(): number { return this.left + this.width; }
33
+ public get bottom(): number { return this.top + this.height; }
34
+
35
+ /**
36
+ * create a rectangle from an object that looks
37
+ * like a rectangle, probably a serialized object
38
+ */
39
+ public static Create(obj: Partial<Rectangle>): Rectangle {
40
+ return new Rectangle(
41
+ obj.left || 0,
42
+ obj.top || 0,
43
+ obj.width || 0,
44
+ obj.height || 0);
45
+ }
46
+
47
+ public static IsRectangle(obj: unknown): obj is IRectangle {
48
+ return (typeof obj === 'object') &&
49
+ (typeof (obj as any)?.left === 'number') &&
50
+ (typeof (obj as any)?.top === 'number') &&
51
+ (typeof (obj as any)?.width === 'number') &&
52
+ (typeof (obj as any)?.height === 'number');
53
+ }
54
+
55
+ constructor( public left = 0,
56
+ public top = 0,
57
+ public width = 0,
58
+ public height = 0 ){}
59
+
60
+ /** returns a new rect shifted from this one by (x,y) */
61
+ public Shift(x = 0, y = 0): Rectangle {
62
+ return new Rectangle(this.left + x, this.top + y, this.width, this.height );
63
+ }
64
+
65
+ public Scale(scale_x = 1, scale_y = scale_x): Rectangle {
66
+ return new Rectangle(this.left * scale_x, this.top * scale_y, this.width * scale_x, this.height * scale_y);
67
+ }
68
+
69
+ /** returns a new rect expanded from this one by (x,y) */
70
+ public Expand(x = 0, y = 0): Rectangle {
71
+ return new Rectangle(this.left, this.top, this.width + x, this.height + y );
72
+ }
73
+
74
+ /** returns a new rectangle that combines this rectangle with the argument */
75
+ public Combine(rect: Rectangle): Rectangle {
76
+ return new Rectangle(
77
+ Math.min(this.left, rect.left),
78
+ Math.min(this.top, rect.top),
79
+ Math.max(this.right, rect.right) - Math.min(this.left, rect.left),
80
+ Math.max(this.bottom, rect.bottom) - Math.min(this.top, rect.top),
81
+ );
82
+ }
83
+
84
+ public CheckEdges(x: number, y: number, border = 16): number{
85
+
86
+ let edge = 0;
87
+
88
+ // tslint:disable-next-line:no-bitwise
89
+ if (x - this.left < border) edge |= 1;
90
+
91
+ // tslint:disable-next-line:no-bitwise
92
+ if (this.right - x < border) edge |= 2;
93
+
94
+ // tslint:disable-next-line:no-bitwise
95
+ if (y - this.top < border) edge |= 4;
96
+
97
+ // tslint:disable-next-line:no-bitwise
98
+ if (this.bottom - y < border) edge |= 8;
99
+
100
+ return edge;
101
+ }
102
+
103
+ /**
104
+ * check if rectangle contains the given coordinates, optionally with
105
+ * some added padding
106
+ */
107
+ public Contains(x: number, y: number, padding = 0): boolean {
108
+ return (x >= this.left - padding)
109
+ && (x <= (this.left + this.width + padding))
110
+ && (y >= this.top - padding)
111
+ && (y <= (this.top + this.height + padding));
112
+ }
113
+
114
+ /** convenience method for canvas */
115
+ public ContextFill(context: CanvasRenderingContext2D){
116
+ context.fillRect(this.left, this.top, this.width, this.height);
117
+ }
118
+
119
+ /** convenience method for canvas */
120
+ public ContextStroke(context: CanvasRenderingContext2D){
121
+ context.strokeRect(this.left, this.top, this.width, this.height);
122
+ }
123
+
124
+ /** clamp coordinate to rectangle */
125
+ public Clamp(x: number, y: number){
126
+ x = Math.min(Math.max(x, this.left), this.right);
127
+ y = Math.min(Math.max(y, this.top), this.bottom);
128
+ return { x, y };
129
+ }
130
+
131
+ /** convenience method for html element style */
132
+ public ApplyStyle(element: HTMLElement){
133
+ element.style.top = this.top + 'px';
134
+ element.style.left = this.left + 'px';
135
+ element.style.width = this.width + 'px';
136
+ element.style.height = this.height + 'px';
137
+ }
138
+
139
+ public toJSON(){
140
+ return {
141
+ top: this.top, left: this.left, width: this.width, height: this.height,
142
+ };
143
+ }
144
+
145
+ }