Skip to content

Commit

Permalink
Enhance/pdf improvements (logseq#6475)
Browse files Browse the repository at this point in the history
Full-text search, highlights and assets alias support 
It also exposes a plugin API for highlight context menu   
    ```ts
    /**
       * Current it's only available for pdf viewer
       * @param label - displayed name of command
       * @param action - callback for the clickable item
       * @param opts - clearSelection: clear highlight selection when callback invoked
       */
      registerHighlightContextMenuItem: (
        label: string,
        action: SimpleCommandCallback,
        opts?: {
          clearSelection: boolean
        }
      ) => unknown
    ```
  • Loading branch information
xyhp915 authored Oct 9, 2022
1 parent ad4fec8 commit d53ac94
Show file tree
Hide file tree
Showing 50 changed files with 3,928 additions and 1,424 deletions.
2 changes: 2 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
:unresolved-var {:exclude [frontend.util/node-path.basename
frontend.util/node-path.dirname
frontend.util/node-path.join
frontend.util/node-path.extname
frontend.util/node-path.name]}

:consistent-alias
Expand Down Expand Up @@ -89,6 +90,7 @@
promesa.core/loop clojure.core/loop
promesa.core/recur clojure.core/recur
rum.core/defcc rum.core/defc
rum.core/with-context clojure.core/let
rum.core/defcontext clojure.core/def
clojure.test.check.clojure-test/defspec clojure.core/def
clojure.test.check.properties/for-all clojure.core/for
Expand Down
2 changes: 1 addition & 1 deletion deps/graph-parser/src/logseq/graph_parser/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#{:id :custom-id :background-color :background_color :heading :collapsed
:created-at :updated-at :last-modified-at :created_at :last_modified_at
:query-table :query-properties :query-sort-by :query-sort-desc :ls-type
:hl-type :hl-page :hl-stamp :logseq.macro-name :logseq.macro-arguments
:hl-type :hl-page :hl-stamp :hl-color :logseq.macro-name :logseq.macro-arguments
:logseq.tldraw.page :logseq.tldraw.shape}
(set (map keyword markers))
@built-in-extended-properties))
Expand Down
3 changes: 2 additions & 1 deletion libs/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/
webpack.*
webpack.*
.DS_Store
22 changes: 18 additions & 4 deletions libs/src/LSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export type SlashCommandActionCmd =
| 'editor/clear-current-slash'
| 'editor/restore-saved-cursor'
export type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
export type SimpleCommandCallback = (e: IHookEvent) => void
export type SimpleCommandCallback<E = any> = (e: IHookEvent & E) => void
export type BlockCommandCallback = (
e: IHookEvent & { uuid: BlockUUID }
) => Promise<void>
Expand Down Expand Up @@ -487,15 +487,29 @@ export interface IEditorProxy extends Record<string, any> {
) => unknown

/**
* register a custom command in the block context menu (triggered by right clicking the block dot)
* @param tag - displayed name of command
* register a custom command in the block context menu (triggered by right-clicking the block dot)
* @param label - displayed name of command
* @param action - can be a single callback function to run when the command is called
*/
registerBlockContextMenuItem: (
tag: string,
label: string,
action: BlockCommandCallback
) => unknown

/**
* Current it's only available for pdf viewer
* @param label - displayed name of command
* @param action - callback for the clickable item
* @param opts - clearSelection: clear highlight selection when callback invoked
*/
registerHighlightContextMenuItem: (
label: string,
action: SimpleCommandCallback,
opts?: {
clearSelection: boolean
}
) => unknown

// block related APIs

checkEditing: () => Promise<BlockUUID | boolean>
Expand Down
34 changes: 29 additions & 5 deletions libs/src/LSPlugin.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ function registerSimpleCommand(
desc?: string
palette?: boolean
keybinding?: SimpleCommandKeybinding
extras?: Record<string, any>
},
action: SimpleCommandCallback
) {
if (typeof action !== 'function') {
return false
}

const { key, label, desc, palette, keybinding } = opts
const { key, label, desc, palette, keybinding, extras } = opts
const eventKey = `SimpleCommandHook${key}${++registeredCmdUid}`

this.Editor['on' + eventKey](action)
Expand All @@ -85,7 +86,7 @@ function registerSimpleCommand(
method: 'register-plugin-simple-command',
args: [
this.baseInfo.id,
[{ key, label, type, desc, keybinding }, ['editor/hook', eventKey]],
[{ key, label, type, desc, keybinding, extras}, ['editor/hook', eventKey]],
palette,
],
})
Expand Down Expand Up @@ -243,15 +244,14 @@ const editor: Partial<IEditorProxy> = {

registerBlockContextMenuItem(
this: LSPluginUser,
tag: string,
label: string,
action: BlockCommandCallback
) {
if (typeof action !== 'function') {
return false
}

const key = tag + '_' + this.baseInfo.id
const label = tag
const key = label + '_' + this.baseInfo.id
const type = 'block-context-menu-item'

registerSimpleCommand.call(
Expand All @@ -265,6 +265,30 @@ const editor: Partial<IEditorProxy> = {
)
},

registerHighlightContextMenuItem(
this: LSPluginUser,
label: string,
action: SimpleCommandCallback,
opts?: { clearSelection: boolean }) {
if (typeof action !== 'function') {
return false
}

const key = label + '_' + this.baseInfo.id
const type = 'highlight-context-menu-item'

registerSimpleCommand.call(
this,
type,
{
key,
label,
extras: opts
},
action
)
},

scrollToBlockInPage(
this: LSPluginUser,
pageName: BlockPageName,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"cljs:release-electron": "clojure -M:cljs release app electron --debug && clojure -M:cljs release publishing",
"cljs:release-app": "clojure -M:cljs release app --config-merge \"{:compiler-options {:output-feature-set :es6}}\"",
"cljs:release-android-app": "clojure -M:cljs release app --config-merge \"{:compiler-options {:output-feature-set :es6}}\"",
"cljs:release-publishing": "clojure -M:cljs release publishing",
"cljs:test": "clojure -M:test compile test",
"cljs:run-test": "node static/tests.js",
"cljs:dev-release-app": "clojure -M:cljs release app --config-merge \"{:closure-defines {frontend.config/DEV-RELEASE true}}\"",
Expand Down Expand Up @@ -132,7 +133,8 @@
"send-intent": "3.0.11",
"threads": "1.6.5",
"url": "^0.11.0",
"yargs-parser": "20.2.4"
"yargs-parser": "20.2.4",
"path-complete-extname": "1.0.0"
},
"resolutions": {
"pixi-graph-fork/@pixi/app": "6.2.0",
Expand Down
3 changes: 1 addition & 2 deletions resources/js/pdfjs/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16421,5 +16421,4 @@ const pdfjsBuild = 'eaaa8b4ad';
/******/ return __webpack_exports__;
/******/ })()
;
});
//# sourceMappingURL=pdf.js.map
});
3 changes: 1 addition & 2 deletions resources/js/pdfjs/pdf.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74427,5 +74427,4 @@ const pdfjsBuild = 'eaaa8b4ad';
/******/ return __webpack_exports__;
/******/ })()
;
});
//# sourceMappingURL=pdf.worker.js.map
});
5 changes: 2 additions & 3 deletions resources/js/pdfjs/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6327,7 +6327,7 @@ const FindState = {
};
exports.FindState = FindState;
const FIND_TIMEOUT = 250;
const MATCH_SCROLL_OFFSET_TOP = -50;
const MATCH_SCROLL_OFFSET_TOP = -200;
const MATCH_SCROLL_OFFSET_LEFT = -400;
const CHARACTERS_TO_NORMALIZE = {
"\u2010": "-",
Expand Down Expand Up @@ -8562,5 +8562,4 @@ const pdfjsBuild = 'eaaa8b4ad';
/******/ return __webpack_exports__;
/******/ })()
;
});
//# sourceMappingURL=pdf_viewer.js.map
});
6 changes: 1 addition & 5 deletions resources/js/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ contextBridge.exposeInMainWorld('apis', {

const dest = path.join(repoPathRoot, to)
const assetsRoot = path.dirname(dest)

if (!/assets$/.test(assetsRoot)) {
throw new Error('illegal assets dirname')
}


await fs.promises.mkdir(assetsRoot, { recursive: true })

from = from && decodeURIComponent(from || getFilePathFromClipboard())
Expand Down
28 changes: 20 additions & 8 deletions src/electron/electron/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [electron.handler :as handler]
[electron.search :as search]
[electron.updater :refer [init-updater] :as updater]
[electron.utils :refer [*win mac? linux? dev? get-win-from-sender restore-user-fetch-agent get-graph-name]]
[electron.utils :refer [*win mac? linux? dev? get-win-from-sender restore-user-fetch-agent
decode-protected-assets-schema-path get-graph-name send-to-renderer]]
[electron.url :refer [logseq-url-handler]]
[electron.logger :as logger]
[clojure.string :as string]
Expand All @@ -24,6 +25,7 @@
;; Keep same as main/frontend.util.url
(defonce LSP_SCHEME "logseq")
(defonce FILE_LSP_SCHEME "lsp")
(defonce FILE_ASSETS_SCHEME "assets")
(defonce LSP_PROTOCOL (str FILE_LSP_SCHEME "://"))
(defonce PLUGIN_URL (str LSP_PROTOCOL "logseq.io/"))
(defonce STATIC_URL (str LSP_PROTOCOL "logseq.com/"))
Expand Down Expand Up @@ -57,11 +59,12 @@
(.setAsDefaultProtocolClient app LSP_SCHEME)

(.registerFileProtocol
protocol "assets"
protocol FILE_ASSETS_SCHEME
(fn [^js request callback]
(let [url (.-url request)
path (string/replace url "assets://" "")
path (js/decodeURI path)]
url (decode-protected-assets-schema-path url)
path (js/decodeURI url)
path (string/replace path "assets://" "")]
(callback #js {:path path}))))

(.registerFileProtocol
Expand All @@ -81,7 +84,7 @@

#(do
(.unregisterProtocol protocol FILE_LSP_SCHEME)
(.unregisterProtocol protocol "assets")))
(.unregisterProtocol protocol FILE_ASSETS_SCHEME)))

(defn- handle-export-publish-assets [_event html custom-css-path export-css-path repo-path asset-filenames output-path]
(p/let [app-path (. app getAppPath)
Expand Down Expand Up @@ -132,8 +135,12 @@
;; TODO: ugly, replace with ls-files and filter with ".map"
_ (p/all (map (fn [file]
(. fs removeSync (path/join static-dir "js" (str file ".map"))))
["main.js" "code-editor.js" "excalidraw.js" "tldraw.js" "age-encryption.js"]))]
(. dialog showMessageBox (clj->js {:message (str "Export public pages and publish assets to " root-dir " successfully")})))))))
["main.js" "code-editor.js" "excalidraw.js" "age-encryption.js"]))]

(send-to-renderer
:notification
{:type "success"
:payload (str "Export public pages and publish assets to " root-dir " successfully 🎉")}))))))

(defn setup-app-manager!
[^js win]
Expand Down Expand Up @@ -261,7 +268,12 @@
protocol (bean/->js [{:scheme LSP_SCHEME
:privileges privileges}
{:scheme FILE_LSP_SCHEME
:privileges privileges}]))
:privileges privileges}
{:scheme FILE_ASSETS_SCHEME
:privileges {:standard false
:secure false
:bypassCSP false
:supportFetchAPI false}}]))

(set-app-menu!)
(setup-deeplink!)
Expand Down
1 change: 0 additions & 1 deletion src/electron/electron/file_sync_rsapi.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@
(when-not (.isDestroyed win)
(.. win -webContents
(send progress-notify-chan (bean/->js progress-info))))))))

6 changes: 6 additions & 0 deletions src/electron/electron/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@
[graph-dir]
(str "logseq_local_" graph-dir))

(defn decode-protected-assets-schema-path
[schema-path]
(cond-> schema-path
(string? schema-path)
(string/replace "/logseq__colon/" ":/")))

;; Keep update with the normalization in main
(defn normalize
[s]
Expand Down
Loading

0 comments on commit d53ac94

Please sign in to comment.