Notes/src/public/app/widgets/bulk_actions/execute_script.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-01-20 21:45:30 +01:00
import SpacedUpdate from "../../services/spaced_update.js";
import AbstractBulkAction from "./abstract_bulk_action.js";
2021-01-20 21:45:30 +01:00
const TPL = `
<tr>
<td>
Execute script:
</td>
<td>
2021-01-26 14:10:34 +01:00
<input type="text"
class="form-control script"
placeholder="note.title = note.title + '- suffix';"/>
2021-01-20 21:45:30 +01:00
</td>
2021-01-26 14:10:34 +01:00
<td class="button-column">
2021-01-26 15:54:41 +01:00
<div style="display: flex">
<div class="dropdown help-dropdown">
<span class="bx bx-help-circle icon-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
<div class="dropdown-menu dropdown-menu-right p-4">
You can execute simple scripts on the matched notes.
For example to append a string to a note's title, use this small script:
<pre>note.title = note.title + ' - suffix';</pre>
2021-01-26 22:22:17 +01:00
More complex example would be deleting all matched note's attributes:
<pre>for (const attr of note.getOwnedAttributes) { attr.markAsDeleted(); }</pre>
2021-01-26 15:54:41 +01:00
</div>
</div>
<span class="bx bx-x icon-action action-conf-del"></span>
</div>
2021-01-20 21:45:30 +01:00
</td>
</tr>`;
export default class ExecuteScriptBulkAction extends AbstractBulkAction {
2021-01-20 21:45:30 +01:00
static get actionName() { return "executeScript"; }
2022-06-03 17:29:08 +02:00
static get actionTitle() { return "Execute script"; }
2021-01-20 21:45:30 +01:00
doRender() {
const $action = $(TPL);
const $script = $action.find('.script');
$script.val(this.actionDef.script || "");
const spacedUpdate = new SpacedUpdate(async () => {
await this.saveAction({ script: $script.val() });
}, 1000)
$script.on('input', () => spacedUpdate.scheduleUpdate());
return $action;
}
}