mirror of
https://github.com/logseq/logseq.git
synced 2026-02-01 22:47:36 +00:00
enhance(plugin): basic plugin setup for web platform
This commit is contained in:
@@ -50,6 +50,7 @@
|
||||
<script defer src="/static/js/highlight.min.js"></script>
|
||||
<script defer src="/static/js/interact.min.js"></script>
|
||||
<script defer src="/static/js/marked.min.js"></script>
|
||||
<script defer src="/static/js/eventemitter3.umd.min.js"></script>
|
||||
<script defer src="/static/js/html2canvas.min.js"></script>
|
||||
<script defer src="/static/js/react.production.min.js"></script>
|
||||
<script defer src="/static/js/react-dom.production.min.js"></script>
|
||||
|
||||
@@ -49,6 +49,7 @@ const portal = new MagicPortal(worker);
|
||||
<script defer src="./js/highlight.min.js"></script>
|
||||
<script defer src="./js/interact.min.js"></script>
|
||||
<script defer src="./js/marked.min.js"></script>
|
||||
<script defer src="./js/eventemitter3.umd.min.js"></script>
|
||||
<script defer src="./js/html2canvas.min.js"></script>
|
||||
<script defer src="./js/lsplugin.core.js"></script>
|
||||
<script defer src="./js/react.production.min.js"></script>
|
||||
|
||||
1
resources/js/eventemitter3.umd.min.js
vendored
Normal file
1
resources/js/eventemitter3.umd.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).EventEmitter3=t()}(this,(function(){"use strict";var e={};return function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,s||e,i),f=n?n+t:t;return e._events[f]?e._events[f].fn?e._events[f]=[e._events[f],c]:e._events[f].push(c):(e._events[f]=c,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,s=r.length,i=new Array(s);o<s;o++)i[o]=r[o].fn;return i},c.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},c.prototype.emit=function(e,t,r,o,s,i){var c=n?n+e:e;if(!this._events[c])return!1;var f,u,a=this._events[c],l=arguments.length;if(a.fn){switch(a.once&&this.removeListener(e,a.fn,void 0,!0),l){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,t),!0;case 3:return a.fn.call(a.context,t,r),!0;case 4:return a.fn.call(a.context,t,r,o),!0;case 5:return a.fn.call(a.context,t,r,o,s),!0;case 6:return a.fn.call(a.context,t,r,o,s,i),!0}for(u=1,f=new Array(l-1);u<l;u++)f[u-1]=arguments[u];a.fn.apply(a.context,f)}else{var p,v=a.length;for(u=0;u<v;u++)switch(a[u].once&&this.removeListener(e,a[u].fn,void 0,!0),l){case 1:a[u].fn.call(a[u].context);break;case 2:a[u].fn.call(a[u].context,t);break;case 3:a[u].fn.call(a[u].context,t,r);break;case 4:a[u].fn.call(a[u].context,t,r,o);break;default:if(!f)for(p=1,f=new Array(l-1);p<l;p++)f[p-1]=arguments[p];a[u].fn.apply(a[u].context,f)}}return!0},c.prototype.on=function(e,t,n){return s(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return s(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,r,o){var s=n?n+e:e;if(!this._events[s])return this;if(!t)return i(this,s),this;var c=this._events[s];if(c.fn)c.fn!==t||o&&!c.once||r&&c.context!==r||i(this,s);else{for(var f=0,u=[],a=c.length;f<a;f++)(c[f].fn!==t||o&&!c[f].once||r&&c[f].context!==r)&&u.push(c[f]);u.length?this._events[s]=1===u.length?u[0]:u:i(this,s)}return this},c.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&i(this,t)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c}({get exports(){return e},set exports(t){e=t}}),e}));//# sourceMappingURL=eventemitter3.umd.min.js.map
|
||||
@@ -640,10 +640,12 @@
|
||||
(storage/set ::storage-spec/lsp-core-enabled v))]
|
||||
[:div.flex.items-center.gap-2
|
||||
(ui/toggle on? on-toggle true)
|
||||
(when (not= (boolean value) on?)
|
||||
(ui/button (t :plugin/restart)
|
||||
:on-click #(js/logseq.api.relaunch)
|
||||
:small? true :intent "logseq"))]))
|
||||
|
||||
(when (util/electron?)
|
||||
(when (not= (boolean value) on?)
|
||||
(ui/button (t :plugin/restart)
|
||||
:on-click #(js/logseq.api.relaunch)
|
||||
:small? true :intent "logseq")))]))
|
||||
|
||||
(rum/defc http-server-enabled-switcher
|
||||
[t]
|
||||
@@ -1112,7 +1114,7 @@
|
||||
(when (= "Enter" (util/ekey e))
|
||||
(update-home-page e)))}]]]])
|
||||
(when-not db-based? (whiteboards-switcher-row enable-whiteboards?))
|
||||
(when (and (util/electron?) config/feature-plugin-system-on?)
|
||||
(when (and web-platform? config/feature-plugin-system-on?)
|
||||
(plugin-system-switcher-row))
|
||||
(when (util/electron?)
|
||||
(http-server-switcher-row))
|
||||
|
||||
@@ -85,11 +85,10 @@
|
||||
|
||||
(rum/use-effect!
|
||||
#(when config/lsp-enabled?
|
||||
(plugin-handler/setup-install-listener!)
|
||||
(plugin-config-handler/setup-install-listener!)
|
||||
(plugin-handler/load-plugin-preferences)
|
||||
(fn []
|
||||
(js/window.apis.removeAllListeners (name :lsp-updates))))
|
||||
(comp
|
||||
(plugin-handler/setup-install-listener!)
|
||||
(plugin-config-handler/setup-install-listener!)))
|
||||
[])
|
||||
|
||||
(rum/use-effect!
|
||||
|
||||
@@ -74,9 +74,9 @@
|
||||
|
||||
;; User level configuration for whether plugins are enabled
|
||||
(defonce lsp-enabled?
|
||||
(and (util/electron?)
|
||||
(not (false? feature-plugin-system-on?))
|
||||
(state/lsp-enabled?-or-theme)))
|
||||
(and util/plugin-platform?
|
||||
(not (false? feature-plugin-system-on?))
|
||||
(state/lsp-enabled?-or-theme)))
|
||||
|
||||
(defn plugin-config-enabled?
|
||||
[]
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
(defn invoke-exported-api
|
||||
[type & args]
|
||||
(try
|
||||
(apply js-invoke (aget js/window.logseq "api") type args)
|
||||
(apply js-invoke (aget js/window.logseq "api") (name type) args)
|
||||
(catch :default e (js/console.error e))))
|
||||
|
||||
(defn markdown-to-html
|
||||
@@ -54,9 +54,16 @@
|
||||
(defonce stats-url (str central-endpoint "stats.json"))
|
||||
(declare select-a-plugin-theme)
|
||||
|
||||
(defn setup-global-apis-for-web!
|
||||
[]
|
||||
(when (and util/web-platform?
|
||||
(nil? js/window.apis))
|
||||
(let [^js e (js/window.EventEmitter3.)]
|
||||
(set! (. js/window -apis) e))))
|
||||
|
||||
(defn load-plugin-preferences
|
||||
[]
|
||||
(-> (invoke-exported-api "load_user_preferences")
|
||||
(-> (invoke-exported-api :load_user_preferences)
|
||||
(p/then #(bean/->clj %))
|
||||
(p/then #(state/set-state! :plugin/preferences %))
|
||||
(p/catch
|
||||
@@ -249,8 +256,10 @@
|
||||
;; reset
|
||||
(js/setTimeout #(state/set-state! :plugin/installing nil) 512)
|
||||
true)]
|
||||
|
||||
(js/window.apis.addListener channel listener)))
|
||||
(js/window.apis.addListener channel listener)
|
||||
;; teardown
|
||||
(fn []
|
||||
(js/window.apis.removeListener channel listener))))
|
||||
|
||||
(defn- normalize-plugin-metadata
|
||||
[metadata]
|
||||
@@ -853,7 +862,9 @@
|
||||
[callback]
|
||||
(if (not config/lsp-enabled?)
|
||||
(callback)
|
||||
(init-plugins! callback)))
|
||||
(do
|
||||
(setup-global-apis-for-web!)
|
||||
(init-plugins! callback))))
|
||||
|
||||
(comment
|
||||
{:pending (count (:plugin/updates-pending @state/state))
|
||||
|
||||
@@ -110,18 +110,22 @@ returns map of plugins to install and uninstall"
|
||||
(defn setup-install-listener!
|
||||
"Sets up a listener for the lsp-installed event to update plugins.edn"
|
||||
[]
|
||||
(let [listener (fn listener [_ e]
|
||||
(let [channel (name :lsp-updates)
|
||||
listener (fn listener [_ e]
|
||||
(when-let [{:keys [status payload only-check]} (bean/->clj e)]
|
||||
(when (and (= status "completed") (not only-check))
|
||||
(let [{:keys [theme effect]} payload]
|
||||
(add-or-update-plugin
|
||||
(assoc payload
|
||||
:version (:installed-version payload)
|
||||
:effect (boolean effect)
|
||||
;; Manual installation doesn't have theme field but
|
||||
;; plugin.edn requires this field
|
||||
:theme (boolean theme)))))))]
|
||||
(js/window.apis.addListener (name :lsp-updates) listener)))
|
||||
(assoc payload
|
||||
:version (:installed-version payload)
|
||||
:effect (boolean effect)
|
||||
;; Manual installation doesn't have theme field but
|
||||
;; plugin.edn requires this field
|
||||
:theme (boolean theme)))))))]
|
||||
(js/window.apis.addListener channel listener)
|
||||
;;teardown
|
||||
(fn []
|
||||
(js/window.apis.removeListener channel listener))))
|
||||
|
||||
(defn start
|
||||
"This component has just one responsibility on start, to create a plugins.edn
|
||||
|
||||
@@ -202,7 +202,7 @@
|
||||
:mobile/app-state-change (atom nil)
|
||||
|
||||
;; plugin
|
||||
:plugin/enabled (and (util/electron?)
|
||||
:plugin/enabled (and util/plugin-platform?
|
||||
;; true false :theme-only
|
||||
((fnil identity true) (storage/get ::storage-spec/lsp-core-enabled)))
|
||||
:plugin/preferences nil
|
||||
|
||||
@@ -146,7 +146,8 @@
|
||||
(do
|
||||
(def nfs? (and (not (electron?))
|
||||
(not (mobile-util/native-platform?))))
|
||||
(def web-platform? nfs?)))
|
||||
(def web-platform? nfs?)
|
||||
(def plugin-platform? (or web-platform? (electron?)))))
|
||||
|
||||
#?(:cljs
|
||||
(defn file-protocol?
|
||||
|
||||
@@ -2,7 +2,6 @@ import path from 'path/path.js'
|
||||
|
||||
// TODO split the capacitor abilities to a separate file for capacitor APIs
|
||||
import { Capacitor } from '@capacitor/core'
|
||||
import { StatusBar, Style } from '@capacitor/status-bar'
|
||||
import { Clipboard as CapacitorClipboard } from '@capacitor/clipboard'
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
|
||||
@@ -354,13 +354,18 @@
|
||||
|
||||
(def ^:export load_user_preferences
|
||||
(fn []
|
||||
(p/let [repo ""
|
||||
path (plugin-handler/get-ls-dotdir-root)
|
||||
path (util/node-path.join path "preferences.json")
|
||||
_ (fs/create-if-not-exists repo nil path)
|
||||
json (fs/read-file nil path)
|
||||
json (if (string/blank? json) "{}" json)]
|
||||
(js/JSON.parse json))))
|
||||
(if (util/electron?)
|
||||
(p/let [repo ""
|
||||
path (plugin-handler/get-ls-dotdir-root)
|
||||
path (util/node-path.join path "preferences.json")
|
||||
_ (fs/create-if-not-exists repo nil path)
|
||||
json (fs/read-file nil path)
|
||||
json (if (string/blank? json) "{}" json)]
|
||||
(js/JSON.parse json))
|
||||
;; TODO: for web
|
||||
(do
|
||||
(js/console.warn "==> plugin:" "load user preferences for Web!")
|
||||
#js {}))))
|
||||
|
||||
(def ^:export save_user_preferences
|
||||
(fn [^js data]
|
||||
|
||||
Reference in New Issue
Block a user