Prechádzať zdrojové kódy

feat: add plugin methods

Gerald 7 rokov pred
rodič
commit
9325367438

+ 6 - 0
src/background/plugin/events.js

@@ -0,0 +1,6 @@
+import EventEmitter from '#/common/events';
+
+export default new EventEmitter([
+  'scriptEdit',
+  'scriptChanged',
+]);

+ 21 - 0
src/background/plugin/index.js

@@ -0,0 +1,21 @@
+import { parseScript, getScripts, getScriptCode } from '../utils/db';
+
+// eslint-disable-next-line import/prefer-default-export
+export const script = {
+  update({ id, code }) {
+    // Update an existing script by ID
+    return parseScript({ id, code });
+  },
+  add({ code }) {
+    // Set id=-1 to skip checking @name and @namespace
+    return parseScript({ id: -1, code });
+  },
+  list() {
+    // List all available scripts, without script code
+    return getScripts();
+  },
+  get(id) {
+    // Get script code of an existing script
+    return getScriptCode(id);
+  },
+};

+ 7 - 2
src/background/utils/db.js

@@ -8,6 +8,7 @@ import { register } from './init';
 import patchDB from './patch-db';
 import { setOption } from './options';
 import { sendMessageOrIgnore } from './message';
+import pluginEvents from '../plugin/events';
 
 function cacheOrFetch(handle) {
   const requests = {};
@@ -467,6 +468,8 @@ export function getExportData(ids, withValues) {
   });
 }
 
+const CMD_UPDATE = 'UpdateScript';
+const CMD_ADD = 'AddScript';
 export function parseScript(data) {
   const {
     id, code, message, isNew, config, custom, props, update,
@@ -474,7 +477,7 @@ export function parseScript(data) {
   const meta = parseMeta(code);
   if (!meta.name) return Promise.reject(i18n('msgInvalidScript'));
   const result = {
-    cmd: 'UpdateScript',
+    cmd: CMD_UPDATE,
     data: {
       update: {
         message: message == null ? i18n('msgUpdated') : message || '',
@@ -489,7 +492,8 @@ export function parseScript(data) {
       script = Object.assign({}, oldScript);
     } else {
       ({ script } = newScript());
-      result.cmd = 'AddScript';
+      result.cmd = CMD_ADD;
+      result.data.isNew = true;
       result.data.update.message = i18n('msgInstalled');
     }
     script.config = Object.assign({}, script.config, config, {
@@ -515,6 +519,7 @@ export function parseScript(data) {
     Object.assign(result.data.update, script, update);
     result.data.where = { id: script.props.id };
     sendMessageOrIgnore(result);
+    pluginEvents.emit('scriptChanged', result.data);
     return result;
   });
 }

+ 51 - 0
src/common/events.js

@@ -0,0 +1,51 @@
+export default class EventEmitter {
+  constructor(allowed) {
+    this.events = {};
+    this.allowed = allowed;
+  }
+
+  checkType(type) {
+    if (this.allowed && !this.allowed.includes(type)) {
+      throw new Error(`Unknown event type: ${type}`);
+    }
+  }
+
+  on(type, handle) {
+    this.checkType(type);
+    const { events } = this;
+    let handlers = events[type];
+    if (!handlers) {
+      handlers = [];
+      events[type] = handlers;
+    }
+    return () => this.off(type, handle);
+  }
+
+  off(type, handle) {
+    this.checkType(type);
+    const handlers = this.events[type];
+    if (handlers) {
+      const i = handlers.indexOf(handle);
+      if (i >= 0) handlers.splice(i, 1);
+    }
+  }
+
+  emit(type, data) {
+    this.checkType(type);
+    const handlers = this.events[type];
+    if (handlers) {
+      const evt = {
+        type,
+        data,
+        defaultPrevented: false,
+        preventDefault() {
+          evt.defaultPrevented = true;
+        },
+      };
+      handlers.some(handle => {
+        handle(evt);
+        return evt.defaultPrevented;
+      });
+    }
+  }
+}