-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor new note modal, add validation (#21)
- Loading branch information
1 parent
1a76bb4
commit c6671c5
Showing
9 changed files
with
198 additions
and
58 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
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,27 @@ | ||
import { ProgressBarComponent } from "obsidian"; | ||
|
||
export class BouncingProgressBarComponent extends ProgressBarComponent { | ||
private timerId: number; | ||
|
||
constructor(contentEl: HTMLElement) { | ||
super(contentEl); | ||
this.timerId = -1; | ||
this.setDisabled(true); | ||
this.setValue(0); | ||
} | ||
|
||
private update() { | ||
const cur = this.getValue(); | ||
this.setValue(cur + (cur == 100 ? 1 : -1 )); | ||
}; | ||
|
||
start() { | ||
this.setDisabled(false); | ||
this.timerId = window.setInterval(this.update, 10); | ||
}; | ||
|
||
stop() { | ||
if (this.timerId > 0) window.clearInterval(this.timerId); | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { TextComponent } from "obsidian"; | ||
|
||
type TValidator = (input: string) => string | null; | ||
type TOnValidate = (input: string, err: string[]) => void; | ||
|
||
export class ValidatedTextComponent extends TextComponent { | ||
private readonly _validators = new Set<TValidator>(); | ||
private readonly _errList: HTMLUListElement; | ||
private _onValidateCb: TOnValidate; | ||
private _earlyReturn: boolean; | ||
private _minLen: number; | ||
|
||
|
||
constructor(containerEl: HTMLElement) { | ||
super(containerEl); | ||
this._errList = containerEl.createEl("ul"); | ||
this._errList.addClasses(["validation-error"]); | ||
this._earlyReturn = true; | ||
this._minLen = 3; | ||
this._onValidateCb = () => { }; | ||
this.onChange(() => this.validate()); | ||
containerEl.appendChild(this._errList); | ||
} | ||
|
||
setValidationErrorClass(className: string) { | ||
this._errList.addClass(className); | ||
return this; | ||
} | ||
|
||
setStopOnFirstError(val: boolean = true) { | ||
this._earlyReturn = val; | ||
return this; | ||
} | ||
|
||
setMinimumLength(val: number = 3) { | ||
this._minLen = val; | ||
return this; | ||
} | ||
|
||
addValidator(fn: TValidator) { | ||
this._validators.add(fn); | ||
return this; | ||
} | ||
|
||
onValidate(fn: TOnValidate) { | ||
this._onValidateCb = fn; | ||
return this; | ||
} | ||
|
||
validate() { | ||
const input = this.getValue() || ""; | ||
const errs: string[] = []; | ||
|
||
if (input.length >= this._minLen) { | ||
for (const fn of this._validators) { | ||
const err = fn(input); | ||
if (err !== null) | ||
errs.push(err); | ||
if (errs.length > 0 && this._earlyReturn) | ||
break; | ||
} | ||
} | ||
|
||
this._errList.replaceChildren(...errs); | ||
this._onValidateCb(input, errs); | ||
} | ||
} |
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
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