Skip to content

Commit 545c9bf

Browse files
committed
♻️ end: tests + cleanup
1 parent 5a69f63 commit 545c9bf

File tree

9 files changed

+1881
-174
lines changed

9 files changed

+1881
-174
lines changed

package-lock.json

Lines changed: 1817 additions & 134 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
"ignoreCommitPattern": "(🔖|🔨|🧹|changelog|release|Update README).*"
4949
},
5050
"dependencies": {
51+
"@types/jest": "^29.2.0",
5152
"compare-versions": "^4.1.3",
5253
"eta": "^1.12.3",
54+
"jest-environment-jsdom": "^29.2.2",
5355
"random-word-slugs": "^0.1.6",
5456
"randomcolor": "^0.6.2"
5557
}

src/@types/index.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export interface SnippetorSettings {
32
snippets: Record<string, SnippetConfig>;
43
}
@@ -13,7 +12,7 @@ export interface SnippetConfig {
1312

1413
export interface TaskSnippetConfig extends SnippetConfig {
1514
taskSettings: TaskSettings[];
16-
baseFontSize: number;
15+
baseFontSize?: number;
1716
borderRadius?: number;
1817
hideColorPicker?: boolean;
1918
styleUncheckedTask?: boolean;
@@ -27,7 +26,7 @@ export interface TaskSettings {
2726
li: TaskListItemSettings;
2827
cache?: {
2928
// not persisted
30-
i: number;
29+
i?: number;
3130
expanded?: boolean;
3231
textEl?: HTMLSpanElement;
3332
itemEl?: HTMLLIElement;

src/snippetor-CreateCheckboxesModal.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ class CreateCheckboxesModal extends Modal {
6363
this.snippetor = snippetor;
6464
this.containerEl.addClass("snippetor-checkboxes-modal");
6565
this.containerEl.id = "snippetor-modal";
66-
this.cfg = taskSnippetCfg || snippetor.createNewTaskSnippetCfg();
6766

6867
// Ensure required config, migrate old task data
69-
this.snippetor.initTaskSnippetConfig(this.cfg);
68+
this.cfg = this.snippetor.initTaskSnippetConfig(
69+
taskSnippetCfg || snippetor.createNewTaskSnippetCfg()
70+
);
7071

7172
// save snapshot of task settings
7273
this.origTaskSettings = JSON.parse(
@@ -97,7 +98,7 @@ class CreateCheckboxesModal extends Modal {
9798
});
9899

99100
this.elements.list = content.createEl("ul", {
100-
cls: "contains-task-list has-list-bullet"
101+
cls: "contains-task-list has-list-bullet",
101102
});
102103

103104
this.showTasks();
@@ -329,10 +330,7 @@ class CreateCheckboxesModal extends Modal {
329330
}
330331
}
331332

332-
drawCheckboxSettings(
333-
ts: TaskSettings,
334-
parent: HTMLSpanElement
335-
): void {
333+
drawCheckboxSettings(ts: TaskSettings, parent: HTMLSpanElement): void {
336334
const settings = parent.createSpan("snippetor-row");
337335
const i = ts.cache.i;
338336

@@ -471,7 +469,9 @@ class CreateCheckboxesModal extends Modal {
471469
fontSize.sliderEl.name = "size-" + i;
472470
new ExtraButtonComponent(sizeGroup)
473471
.setIcon("reset")
474-
.setTooltip("Reset font size to default: " + this.elements.defaultFontSize)
472+
.setTooltip(
473+
"Reset font size to default: " + this.elements.defaultFontSize
474+
)
475475
.onClick(async () => {
476476
fontSize.setValue(this.elements.defaultFontSize);
477477
Reflect.deleteProperty(ts.checkbox.format, "fontSize");
@@ -528,10 +528,7 @@ class CreateCheckboxesModal extends Modal {
528528
);
529529
}
530530

531-
drawTextSettings(
532-
ts: TaskSettings,
533-
parent: HTMLSpanElement
534-
): void {
531+
drawTextSettings(ts: TaskSettings, parent: HTMLSpanElement): void {
535532
const i = ts.cache.i;
536533

537534
const settings = parent.createSpan("snippetor-row text-settings");
@@ -586,7 +583,9 @@ class CreateCheckboxesModal extends Modal {
586583
fontSize.sliderEl.name = "size-" + i;
587584
new ExtraButtonComponent(sizeGroup)
588585
.setIcon("reset")
589-
.setTooltip("Reset font size to default: " + this.elements.defaultFontSize)
586+
.setTooltip(
587+
"Reset font size to default: " + this.elements.defaultFontSize
588+
)
590589
.onClick(async () => {
591590
fontSize.setValue(this.elements.defaultFontSize);
592591
Reflect.deleteProperty(ts.checkbox.format, "fontSize");
@@ -645,10 +644,7 @@ class CreateCheckboxesModal extends Modal {
645644
);
646645
}
647646

648-
drawFontSettings(
649-
ts: TaskSettings,
650-
parent: HTMLSpanElement
651-
): void {
647+
drawFontSettings(ts: TaskSettings, parent: HTMLSpanElement): void {
652648
const i = ts.cache.i;
653649

654650
const settings = parent.createSpan("snippetor-row text-settings");
@@ -678,7 +674,6 @@ class CreateCheckboxesModal extends Modal {
678674
});
679675
textFont.inputEl.addClass("snippetor-font-setting");
680676

681-
682677
// List Item font
683678
const chkboxGroup = settings.createSpan(
684679
"snippetor-group decorated checkbox-font"
@@ -690,7 +685,8 @@ class CreateCheckboxesModal extends Modal {
690685
this.snippetor.initialize(ts, "checkbox", "format");
691686
const boxFont = new TextComponent(chkboxGroup)
692687
.setValue(
693-
ts.checkbox.format === undefined || ts.checkbox.format.font === undefined
688+
ts.checkbox.format === undefined ||
689+
ts.checkbox.format.font === undefined
694690
? ""
695691
: ts.checkbox.format.font
696692
)
@@ -841,7 +837,6 @@ class CreateCheckboxesModal extends Modal {
841837
applySettingsToElements(taskSettings: TaskSettings): void {
842838
const itemEl = taskSettings.cache.itemEl;
843839
const checkboxEl = taskSettings.cache.checkboxEl;
844-
const textEl = taskSettings.cache.textEl;
845840

846841
// data-line attribute
847842
itemEl.setAttribute("data-line", taskSettings.cache.i + "");
@@ -881,6 +876,9 @@ class CreateCheckboxesModal extends Modal {
881876
if (taskSettings.li.format && taskSettings.li.format.fontSize) {
882877
fontSize = taskSettings.li.format.fontSize + "px";
883878
}
879+
if (taskSettings.li.format && taskSettings.li.format.font) {
880+
font = taskSettings.li.format.font;
881+
}
884882

885883
textEl.style.setProperty("--snippetor-text-decoration", decoration);
886884
textEl.style.setProperty("--snippetor-text-font", font);
@@ -914,17 +912,22 @@ class CreateCheckboxesModal extends Modal {
914912
COLOR.BACKGROUND
915913
);
916914

917-
li.style.setProperty("--checkbox-color",
915+
li.style.setProperty(
916+
"--checkbox-color",
918917
boxFgColor === "inherit" ? "var(--text-normal)" : boxFgColor
919918
);
920919

921920
li.style.setProperty("--checkbox-bg", boxBgColor);
922-
li.style.setProperty("--checkbox-border-color",
921+
li.style.setProperty(
922+
"--checkbox-border-color",
923923
taskSettings.checkbox.hideBorder ? "transparent" : boxFgColor
924924
);
925925

926926
if (this.cfg.borderRadius) {
927-
li.style.setProperty("--checkbox-radius", this.cfg.borderRadius + "%");
927+
li.style.setProperty(
928+
"--checkbox-radius",
929+
this.cfg.borderRadius + "%"
930+
);
928931
} else {
929932
li.style.removeProperty("--checkbox-radius");
930933
}
@@ -934,7 +937,6 @@ class CreateCheckboxesModal extends Modal {
934937
}
935938

936939
setCheckboxStyles(taskSettings: TaskSettings): void {
937-
const li = taskSettings.cache.itemEl;
938940
const checkbox = taskSettings.cache.checkboxEl;
939941

940942
// content value for snippetor
@@ -949,17 +951,22 @@ class CreateCheckboxesModal extends Modal {
949951
if (taskSettings.checkbox.format && taskSettings.checkbox.format.font) {
950952
font = taskSettings.checkbox.format.font;
951953
}
952-
if (taskSettings.checkbox.format && taskSettings.checkbox.format.fontSize) {
954+
if (
955+
taskSettings.checkbox.format &&
956+
taskSettings.checkbox.format.fontSize
957+
) {
953958
fontSize = taskSettings.checkbox.format.fontSize + "px";
954959
}
955960

956961
checkbox.style.setProperty("--snippetor-box-font", font);
957962
checkbox.style.setProperty("--snippetor-box-font-size", fontSize);
958963

959-
checkbox.style.setProperty("--snippetor-box-top",
964+
checkbox.style.setProperty(
965+
"--snippetor-box-top",
960966
(taskSettings.checkbox.top ? taskSettings.checkbox.top : 0) + "px"
961967
);
962-
checkbox.style.setProperty("--snippetor-box-left",
968+
checkbox.style.setProperty(
969+
"--snippetor-box-left",
963970
(taskSettings.checkbox.left ? taskSettings.checkbox.left : 0) + "px"
964971
);
965972
}

src/snippetor-CreateFoldersModal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ class CreateFolderModal extends Modal {
6868
this.snippetor = snippetor;
6969
this.containerEl.addClass("snippetor-folders-modal");
7070
this.containerEl.id = "snippetor-modal";
71-
this.cfg = folderSnippetCfg || snippetor.createNewFolderSnippetCfg();
7271

7372
// Ensure required config, migrate old data
74-
this.snippetor.initCommonConfig(this.cfg);
73+
this.cfg = this.snippetor.initFolderSnippetConfig(
74+
folderSnippetCfg || snippetor.createNewFolderSnippetCfg()
75+
);
7576

7677
// save snapshot of settings
7778
this.original = JSON.parse(JSON.stringify(this.cfg));
@@ -119,7 +120,7 @@ class CreateFolderModal extends Modal {
119120
.createDiv("nav-folder-children");
120121

121122
// this will be cleared/emptied with showFolders
122-
let basicFolderTitle = folderEl
123+
const basicFolderTitle = folderEl
123124
.createDiv("nav-folder")
124125
.createDiv("nav-folder-title")
125126
.createDiv("nav-folder-title-content");

src/snippetor-Defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const DEFAULT_FOLDER_SNIPPET_SETTINGS: Partial<FolderSnippetConfig> = {
3434
default: {
3535
cache: {
3636
folderEl: null,
37+
titleEl: null,
3738
},
3839
target: "",
3940
lightMode: {

src/snippetor-ModalHelper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export class ModalHelper {
388388
return true;
389389
}
390390

391-
createFilenameSetting(content: HTMLDivElement, cfg: SnippetConfig) {
391+
createFilenameSetting(content: HTMLDivElement, cfg: SnippetConfig): void {
392392
new Setting(content)
393393
.setName("Name of generated snippet (filename)")
394394
.setClass("snippet-filename")
@@ -415,7 +415,7 @@ export class ModalHelper {
415415
}
416416

417417
createHtmlStyleElement(cfg: SnippetConfig): HTMLStyleElement {
418-
let style = document.createElement("style");
418+
const style = document.createElement("style");
419419
if (cfg.cssFontImport) {
420420
style.replaceChildren(document.createTextNode(cfg.cssFontImport));
421421
}
@@ -427,8 +427,8 @@ export class ModalHelper {
427427
content: HTMLDivElement,
428428
cfg: SnippetConfig,
429429
style: HTMLStyleElement
430-
) {
431-
let result = new Setting(content)
430+
): void {
431+
const result = new Setting(content)
432432
.setName("Import (CSS) additional fonts")
433433
.setDesc(
434434
"Cut/paste a CSS @import statement to add an additional font to this snippet."

src/snippetor-Snippetor.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Snippetor {
4747
return new Set(["x", "-", ">"]);
4848
}
4949

50-
createNewTaskSnippetCfg(): TaskSnippetConfig {
50+
createNewTaskSnippetCfg(): Partial<TaskSnippetConfig> {
5151
const result = Object.assign({}, DEFAULT_TASK_SNIPPET_SETTINGS, {
5252
id: this.generateId(),
5353
name: generateSlug(2),
@@ -58,7 +58,7 @@ export class Snippetor {
5858
});
5959
return result;
6060
}
61-
createNewFolderSnippetCfg(): FolderSnippetConfig {
61+
createNewFolderSnippetCfg(): Partial<FolderSnippetConfig> {
6262
const result = Object.assign({}, DEFAULT_FOLDER_SNIPPET_SETTINGS, {
6363
id: this.generateId(),
6464
name: generateSlug(2),
@@ -120,6 +120,7 @@ export class Snippetor {
120120
target: v,
121121
cache: {
122122
folderEl: null,
123+
titleEl: null,
123124
},
124125
};
125126
}
@@ -138,7 +139,14 @@ export class Snippetor {
138139
cfg.version = "0.1.8";
139140
}
140141

141-
initTaskSnippetConfig(cfg: Partial<TaskSnippetConfig>): void {
142+
initFolderSnippetConfig(
143+
cfg: Partial<FolderSnippetConfig>
144+
): FolderSnippetConfig {
145+
this.initCommonConfig(cfg); // last, it bumps the version
146+
return cfg as FolderSnippetConfig;
147+
}
148+
149+
initTaskSnippetConfig(cfg: Partial<TaskSnippetConfig>): TaskSnippetConfig {
142150
cfg.taskSettings.forEach((ts) => {
143151
this.initTaskSettings(cfg.version, ts);
144152
});
@@ -150,6 +158,8 @@ export class Snippetor {
150158
cfg.baseFontSize = 14;
151159
}
152160
this.initCommonConfig(cfg); // last, it bumps the version
161+
162+
return cfg as TaskSnippetConfig;
153163
}
154164

155165
initTaskSettings(version: string, ts: Partial<TaskSettings>): void {

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"es5",
1717
"scripthost",
1818
"es2019"
19+
],
20+
"types": [
21+
"mocha",
22+
"jest"
1923
]
2024
},
2125
"include": [

0 commit comments

Comments
 (0)