Browse Source

enhance(plugin): get caller plugin id from host

charlie 1 year ago
parent
commit
a7a19eb88b

+ 3 - 2
deps/db/src/logseq/db/frontend/property.cljs

@@ -322,8 +322,9 @@
 (defn create-user-property-ident-from-name
   "Creates a property :db/ident for a default user namespace.
    NOTE: Only use this when creating a db-ident for a new property."
-  [property-name]
-  (db-ident/create-db-ident-from-name default-user-namespace property-name))
+  ([property-name] (create-user-property-ident-from-name property-name default-user-namespace))
+  ([property-name user-namespace]
+   (db-ident/create-db-ident-from-name user-namespace property-name)))
 
 (defn get-class-ordered-properties
   [class-entity]

+ 4 - 0
libs/src/LSPlugin.core.ts

@@ -55,6 +55,7 @@ declare global {
   interface Window {
     LSPluginCore: LSPluginCore
     DOMPurify: typeof DOMPurify
+    $$callerPluginID: string | undefined
   }
 }
 
@@ -339,6 +340,7 @@ function initApiProxyHandlers(pluginLocal: PluginLocal) {
     let ret: any
 
     try {
+      window.$$callerPluginID = pluginLocal.id
       ret = await invokeHostExportedApi.apply(pluginLocal, [
         payload.method,
         ...payload.args,
@@ -347,6 +349,8 @@ function initApiProxyHandlers(pluginLocal: PluginLocal) {
       ret = {
         [LSPMSG_ERROR_TAG]: e,
       }
+    } finally {
+      window.$$callerPluginID = undefined
     }
 
     if (pluginLocal.shadow) {

+ 4 - 0
src/main/logseq/api.cljs

@@ -1,6 +1,7 @@
 (ns ^:no-doc logseq.api
   (:require [cljs-bean.core :as bean]
             [cljs.reader]
+            [goog.object :as gobj]
             [datascript.core :as d]
             [frontend.db.conn :as conn]
             [logseq.common.util :as common-util]
@@ -76,6 +77,9 @@
   (some-> (state/get-current-repo)
     (config/db-based-graph?)))
 
+(defn- get-caller-plugin-id
+  [] (gobj/get js/window "$$callerPluginID"))
+
 ;; helpers
 (defn ^:export install-plugin-hook
   [pid hook ^js opts]