Skip to content

Commit

Permalink
add after hook
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Mar 19, 2023
1 parent a9d314c commit c129462
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for malle

## 2.1.0

* Add `after` hook, triggered after replacement of element.

## 2.0.0

* The `fun` function now expects a `Promise<string>` as return value
Expand Down
6 changes: 6 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ When instanciating the `Malle` class, you need to give it an `Options` argument.

Available options:

#### after
`Function(original: HTMLElement, event: Event, value: string): boolean`
default: `undefined`

This function will be called after everything is processed.

#### before
`Function(original: HTMLElement, event: Event): boolean`
default: `undefined`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deltablot/malle",
"version": "2.0.0",
"version": "2.1.0",
"description": "Make text elements malleable, without dependencies.",
"main": "dist/main.js",
"typings": "dist/main.d.ts",
Expand Down
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SelectOptions {
}

export interface Options {
after?(original: HTMLElement, event:Event, value: string): boolean;
before?(original: HTMLElement, event:Event): boolean;
cancel?: string;
cancelClasses?: Array<string>;
Expand Down Expand Up @@ -79,6 +80,7 @@ export class Malle {
*/
normalizeOptions(options: Options): Options {
const defaultOptions = {
after: undefined,
before: undefined,
cancel: '',
cancelClasses: [],
Expand Down Expand Up @@ -161,7 +163,12 @@ export class Malle {
this.opt.fun.call(this, this.input.value, this.original, event, this.input).then((value: string) => {
this.original.innerText = this.opt.inputType === InputType.Select ? (this.input as HTMLSelectElement).options[(this.input as HTMLSelectElement).selectedIndex].text : value;
this.form.replaceWith(this.original);
// execute the after hook
if (typeof this.opt.after === 'function') {
return this.opt.after(this.original, event, value);
}
});

return true;
}

Expand Down

0 comments on commit c129462

Please sign in to comment.