added note.executeScript()

This commit is contained in:
zadam 2021-05-07 21:23:10 +02:00
parent f22c76d9fb
commit fddab59265
6 changed files with 253 additions and 10578 deletions

10762
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -58,7 +58,7 @@
"mime-types": "2.1.30",
"multer": "1.4.2",
"node-abi": "2.26.0",
"open": "8.0.6",
"open": "8.0.8",
"portscanner": "2.2.0",
"rand-token": "1.0.1",
"request": "^2.88.2",
@ -80,8 +80,8 @@
},
"devDependencies": {
"cross-env": "7.0.3",
"electron": "13.0.0-beta.18",
"electron-builder": "22.10.5",
"electron": "13.0.0-beta.23",
"electron-builder": "22.11.1",
"electron-packager": "15.2.0",
"electron-rebuild": "2.3.5",
"esm": "3.2.25",
@ -89,8 +89,8 @@
"jsdoc": "3.6.6",
"lorem-ipsum": "2.0.3",
"rcedit": "3.0.0",
"webpack": "5.35.1",
"webpack-cli": "4.6.0"
"webpack": "5.36.2",
"webpack-cli": "4.7.0"
},
"optionalDependencies": {
"electron-installer-debian": "3.1.0"

View File

@ -3,6 +3,7 @@ import noteAttributeCache from "../services/note_attribute_cache.js";
import ws from "../services/ws.js";
import options from "../services/options.js";
import treeCache from "../services/tree_cache.js";
import bundle from "../services/bundle.js";
const LABEL = 'label';
const RELATION = 'relation';
@ -701,6 +702,55 @@ class NoteShort {
const labels = this.getLabels('workspaceTabBackgroundColor');
return labels.length > 0 ? labels[0].value : "";
}
/** @returns {boolean} true if this note is JavaScript (code or attachment) */
isJavaScript() {
return (this.type === "code" || this.type === "file")
&& (this.mime.startsWith("application/javascript")
|| this.mime === "application/x-javascript"
|| this.mime === "text/javascript");
}
/** @returns {boolean} true if this note is HTML */
isHtml() {
return (this.type === "code" || this.type === "file" || this.type === "render") && this.mime === "text/html";
}
/** @returns {string|null} JS script environment - either "frontend" or "backend" */
getScriptEnv() {
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
return "frontend";
}
if (this.type === 'render') {
return "frontend";
}
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
return "backend";
}
return null;
}
async executeScript() {
if (!this.isJavaScript()) {
throw new Error(`Note ${this.noteId} is of type ${this.type} and mime ${this.mime} and thus cannot be executed`);
}
const env = this.getScriptEnv();
if (env === "frontend") {
const bundleService = (await import("../services/bundle.js")).default;
await bundleService.getAndExecuteBundle(this.noteId);
}
else if (env === "backend") {
await server.post('script/run/' + this.noteId);
}
else {
throw new Error(`Unrecognized env type ${env} for note ${this.noteId}`);
}
}
}
export default NoteShort;

View File

@ -216,6 +216,7 @@ export default class Entrypoints extends Component {
return;
}
// TODO: use note.executeScript()
if (note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(note.noteId);
} else if (note.mime.endsWith("env=backend")) {

View File

@ -8,7 +8,7 @@ import treeService from "../../services/tree.js";
import noteCreateService from "../../services/note_create.js";
import AbstractTextTypeWidget from "./abstract_text_type_widget.js";
const ENABLE_INSPECTOR = false;
const ENABLE_INSPECTOR = true;
const mentionSetup = {
feeds: [
@ -121,7 +121,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
});
this.textEditor.model.document.on('change:data', () => this.spacedUpdate.scheduleUpdate());
console.log('glob.isDev', glob.isDev);
if (glob.isDev && ENABLE_INSPECTOR) {
await import(/* webpackIgnore: true */'../../../libraries/ckeditor/inspector.js');
CKEditorInspector.attach(this.textEditor);

View File

@ -14,6 +14,10 @@ button.btn, button.btn-sm {
font-size: inherit;
}
.btn-micro {
padding: 0 10px 0 10px;
}
.input-group-text {
background-color: var(--accented-background-color) !important;
color: var(--muted-text-color) !important;