-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { | ||
App, | ||
Modal, | ||
SearchComponent, | ||
Setting, | ||
TextComponent, | ||
normalizePath, | ||
} from "obsidian"; | ||
import { generateObsidianFilename } from "utils/filename"; | ||
import { FolderTextSuggest } from "utils/ui/settings/folder"; | ||
import { Clock } from "./clock"; | ||
|
||
export type ClockCreateResultType = { | ||
segments: number; | ||
name: string; | ||
fileName: string; | ||
targetFolder: string; | ||
}; | ||
|
||
export class ClockCreateModal extends Modal { | ||
public result: ClockCreateResultType = { | ||
segments: 0, | ||
name: "", | ||
fileName: "", | ||
targetFolder: "", | ||
}; | ||
|
||
public accepted: boolean = false; | ||
|
||
constructor( | ||
app: App, | ||
defaults: Partial<ClockCreateResultType> = {}, | ||
protected readonly onAccept: (arg: { | ||
name: string; | ||
targetFolder: string; | ||
fileName: string; | ||
clock: Clock; | ||
}) => void, | ||
protected readonly onCancel: () => void, | ||
) { | ||
super(app); | ||
Object.assign(this.result, defaults); | ||
} | ||
|
||
onOpen(): void { | ||
this.accepted = false; | ||
|
||
const { contentEl } = this; | ||
new Setting(contentEl).setName("New Clock").setHeading(); | ||
|
||
let fileNameText: TextComponent; | ||
|
||
new Setting(contentEl).setName("Name").addText((text) => | ||
text.onChange((value) => { | ||
this.result.name = value; | ||
// TODO: could add smarter logic to only update if user hasn't made a specific value | ||
fileNameText.setValue(generateObsidianFilename(value)).onChanged(); | ||
}), | ||
); | ||
|
||
new Setting(contentEl).setName("File name").addText( | ||
(text) => | ||
(fileNameText = text.onChange((value) => { | ||
this.result.fileName = value; | ||
})), | ||
); | ||
|
||
let folderComponent!: SearchComponent; | ||
const folderSetting = new Setting(contentEl) | ||
.setName("Target folder") | ||
.addSearch((search) => { | ||
new FolderTextSuggest(this.app, search.inputEl); | ||
folderComponent = search | ||
.setPlaceholder("Choose a folder") | ||
.setValue(this.result.targetFolder) | ||
.onChange((newFolder) => { | ||
this.result.targetFolder = newFolder; | ||
const normalized = normalizePath(newFolder); | ||
if (this.app.vault.getFolderByPath(normalized)) { | ||
folderSetting.setDesc( | ||
`Creating clock in existing folder '${normalized}'`, | ||
); | ||
} else { | ||
folderSetting.setDesc( | ||
`Creating clock in new folder '${normalized}`, | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
new Setting(contentEl).setName("Segments").addSlider((slider) => | ||
slider | ||
.setLimits(2, 12, 1) | ||
.setValue(6) | ||
.setDynamicTooltip() | ||
.onChange((segments) => { | ||
this.result.segments = segments; | ||
}), | ||
); | ||
|
||
folderComponent.onChanged(); | ||
|
||
new Setting(contentEl) | ||
.addButton((btn) => | ||
btn | ||
.setButtonText("Create") | ||
.setCta() | ||
.onClick(() => { | ||
this.accept(); | ||
}), | ||
) | ||
.addButton((btn) => | ||
btn.setButtonText("Cancel").onClick(() => { | ||
this.accepted = false; | ||
this.close(); | ||
}), | ||
); | ||
} | ||
|
||
accept(): void { | ||
this.accepted = true; | ||
this.close(); | ||
this.onAccept({ | ||
name: this.result.name, | ||
fileName: this.result.fileName, | ||
targetFolder: this.result.targetFolder, | ||
clock: Clock.create({ | ||
progress: 0, | ||
segments: this.result.segments, | ||
active: true, | ||
}).unwrap(), | ||
}); | ||
} | ||
|
||
onClose(): void { | ||
this.contentEl.empty(); | ||
if (!this.accepted) { | ||
this.onCancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
/* How we refer to the plugin in block names, property names, etc. */ | ||
export const PLUGIN_SLUG: string = "iron-vault"; | ||
export const PLUGIN_SLUG = "iron-vault"; | ||
|
||
export function pluginPrefixed(name: string): string { | ||
export function pluginPrefixed<N extends string>( | ||
name: N, | ||
): `${typeof PLUGIN_SLUG}-${N}` { | ||
return `${PLUGIN_SLUG}-${name}`; | ||
} | ||
|
||
export const PLUGIN_KIND_FIELD = pluginPrefixed("kind"); | ||
|
||
export const BLOCK_TYPE__TRACK = pluginPrefixed("track"); | ||
export const BLOCK_TYPE__CLOCK = pluginPrefixed("clock"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters