A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1import {Menu} from "../../../plugin/Menu";
2import {transaction} from "../../wysiwyg/transaction";
3import {Constants} from "../../../constants";
4
5const addFormatItem = (options: {
6 menu: Menu,
7 protyle: IProtyle,
8 colId: string,
9 avID: string,
10 format: string,
11 oldFormat: string
12 avPanelElement: Element
13}) => {
14 options.menu.addItem({
15 iconHTML: "",
16 label: getLabelByNumberFormat(options.format),
17 click() {
18 transaction(options.protyle, [{
19 action: "updateAttrViewColNumberFormat",
20 id: options.colId,
21 avID: options.avID,
22 format: options.format,
23 type: "number",
24 }], [{
25 action: "updateAttrViewColNumberFormat",
26 id: options.colId,
27 avID: options.avID,
28 format: options.oldFormat,
29 type: "number",
30 }]);
31 options.avPanelElement.remove();
32 }
33 });
34};
35
36export const formatNumber = (options: {
37 avPanelElement: Element,
38 element: HTMLElement,
39 protyle: IProtyle,
40 colId: string,
41 avID: string,
42 oldFormat: string
43}) => {
44 const menu = new Menu(Constants.MENU_AV_COL_FORMAT_NUMBER);
45 addFormatItem({
46 menu,
47 protyle: options.protyle,
48 colId: options.colId,
49 avID: options.avID,
50 format: "",
51 oldFormat: options.oldFormat,
52 avPanelElement: options.avPanelElement,
53 });
54 addFormatItem({
55 menu,
56 protyle: options.protyle,
57 colId: options.colId,
58 avID: options.avID,
59 format: "commas",
60 oldFormat: options.oldFormat,
61 avPanelElement: options.avPanelElement,
62 });
63 addFormatItem({
64 menu,
65 protyle: options.protyle,
66 colId: options.colId,
67 avID: options.avID,
68 format: "percent",
69 oldFormat: options.oldFormat,
70 avPanelElement: options.avPanelElement,
71 });
72 addFormatItem({
73 menu,
74 protyle: options.protyle,
75 colId: options.colId,
76 avID: options.avID,
77 format: "USD",
78 oldFormat: options.oldFormat,
79 avPanelElement: options.avPanelElement,
80 });
81 addFormatItem({
82 menu,
83 protyle: options.protyle,
84 colId: options.colId,
85 avID: options.avID,
86 format: "CNY",
87 oldFormat: options.oldFormat,
88 avPanelElement: options.avPanelElement,
89 });
90 addFormatItem({
91 menu,
92 protyle: options.protyle,
93 colId: options.colId,
94 avID: options.avID,
95 format: "EUR",
96 oldFormat: options.oldFormat,
97 avPanelElement: options.avPanelElement,
98 });
99 addFormatItem({
100 menu,
101 protyle: options.protyle,
102 colId: options.colId,
103 avID: options.avID,
104 format: "GBP",
105 oldFormat: options.oldFormat,
106 avPanelElement: options.avPanelElement,
107 });
108 addFormatItem({
109 menu,
110 protyle: options.protyle,
111 colId: options.colId,
112 avID: options.avID,
113 format: "JPY",
114 oldFormat: options.oldFormat,
115 avPanelElement: options.avPanelElement,
116 });
117 addFormatItem({
118 menu,
119 protyle: options.protyle,
120 colId: options.colId,
121 avID: options.avID,
122 format: "RUB",
123 oldFormat: options.oldFormat,
124 avPanelElement: options.avPanelElement,
125 });
126 addFormatItem({
127 menu,
128 protyle: options.protyle,
129 colId: options.colId,
130 avID: options.avID,
131 format: "INR",
132 oldFormat: options.oldFormat,
133 avPanelElement: options.avPanelElement,
134 });
135 addFormatItem({
136 menu,
137 protyle: options.protyle,
138 colId: options.colId,
139 avID: options.avID,
140 format: "KRW",
141 oldFormat: options.oldFormat,
142 avPanelElement: options.avPanelElement,
143 });
144 addFormatItem({
145 menu,
146 protyle: options.protyle,
147 colId: options.colId,
148 avID: options.avID,
149 format: "CAD",
150 oldFormat: options.oldFormat,
151 avPanelElement: options.avPanelElement,
152 });
153 addFormatItem({
154 menu,
155 protyle: options.protyle,
156 colId: options.colId,
157 avID: options.avID,
158 format: "CHF",
159 oldFormat: options.oldFormat,
160 avPanelElement: options.avPanelElement,
161 });
162 addFormatItem({
163 menu,
164 protyle: options.protyle,
165 colId: options.colId,
166 avID: options.avID,
167 format: "THB",
168 oldFormat: options.oldFormat,
169 avPanelElement: options.avPanelElement,
170 });
171 addFormatItem({
172 menu,
173 protyle: options.protyle,
174 colId: options.colId,
175 avID: options.avID,
176 format: "AUD",
177 oldFormat: options.oldFormat,
178 avPanelElement: options.avPanelElement,
179 });
180 addFormatItem({
181 menu,
182 protyle: options.protyle,
183 colId: options.colId,
184 avID: options.avID,
185 format: "HKD",
186 oldFormat: options.oldFormat,
187 avPanelElement: options.avPanelElement,
188 });
189 addFormatItem({
190 menu,
191 protyle: options.protyle,
192 colId: options.colId,
193 avID: options.avID,
194 format: "TWD",
195 oldFormat: options.oldFormat,
196 avPanelElement: options.avPanelElement,
197 });
198 addFormatItem({
199 menu,
200 protyle: options.protyle,
201 colId: options.colId,
202 avID: options.avID,
203 format: "MOP",
204 oldFormat: options.oldFormat,
205 avPanelElement: options.avPanelElement,
206 });
207 addFormatItem({
208 menu,
209 protyle: options.protyle,
210 colId: options.colId,
211 avID: options.avID,
212 format: "SGD",
213 oldFormat: options.oldFormat,
214 avPanelElement: options.avPanelElement,
215 });
216 addFormatItem({
217 menu,
218 protyle: options.protyle,
219 colId: options.colId,
220 avID: options.avID,
221 format: "NZD",
222 oldFormat: options.oldFormat,
223 avPanelElement: options.avPanelElement,
224 });
225 const rect = options.element.getBoundingClientRect();
226 menu.open({
227 x: rect.left,
228 y: rect.bottom,
229 h: rect.height,
230 w: rect.width,
231 isLeft: true,
232 });
233};
234
235export const getLabelByNumberFormat = (format: string) => {
236 if ("" === format) {
237 return window.siyuan.languages.numberFormatNone;
238 } else if ("commas" === format) {
239 return window.siyuan.languages.numberFormatCommas;
240 } else if ("percent" === format) {
241 return window.siyuan.languages.numberFormatPercent;
242 }
243
244 return window.siyuan.languages["numberFormat" + format];
245};