improve(pdf): add basic files

pull/2471/head
charlie 2021-07-14 21:54:30 +08:00
parent ac1f68004a
commit 82330fb3f4
8 changed files with 88528 additions and 0 deletions

14827
resources/js/pdfjs/pdf.js Normal file

File diff suppressed because it is too large Load Diff

65293
resources/js/pdfjs/pdf.worker.js vendored Normal file

File diff suppressed because it is too large Load Diff

22
resources/js/pdfjs/pdf.worker.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,158 @@
(ns frontend.extensions.pdf.impls
(:require [rum.core :as rum]
[promesa.core :as p]
[frontend.extensions.pdf.utils :as pdf-utils]))
(defonce ACTIVE_FILE "https://phx-nine.vercel.app/clojure-hopl-iv-final.pdf")
(rum/defc viewer
[^js pdf-document]
(let [*el-ref (rum/create-ref)
[state, set-state!] (rum/use-state {:viewer nil :bus nil :link nil})
[sel-state, set-sel-state!] (rum/use-state {:range nil :collapsed nil :point nil})]
(rum/use-effect!
(fn [] (let [^js event-bus (js/pdfjsViewer.EventBus.)
^js link-service (js/pdfjsViewer.PDFLinkService. #js {:eventBus event-bus :externalLinkTarget 2})
^js el (rum/deref *el-ref)
^js viewer (js/pdfjsViewer.PDFViewer.
#js {:container el
:eventBus event-bus
:linkService link-service
:enhanceTextSelection true
:removePageBorders true})]
(. link-service setDocument pdf-document)
(. link-service setViewer viewer)
;; TODO: debug
(set! (. js/window -lsPdfViewer) viewer)
(p/then (. viewer setDocument pdf-document)
#(set-state! {:viewer viewer :bus event-bus :link link-service})))
;;TODO: destroy
#())
[])
;; selection context menu
(rum/use-effect!
(fn []
(when-let [^js sel-range (and (not (:collapsed sel-state)) (:range sel-state))]
(when-let [page-info (pdf-utils/get-page-from-range sel-range)]
(when-let [sel-rects (pdf-utils/get-range-rects<-page-cnt sel-range (:page-el page-info))]
(let [^js point (:point sel-state)]
;; TODO: debug
(js/console.debug "[Range] ====> [" page-info "]" (.toString sel-range) point)
(js/console.debug "[Rects] ====>" sel-rects))))))
[(:range sel-state)])
;; interaction events
(rum/use-effect!
(fn []
(when-let [^js viewer (:viewer state)]
(let [^js el (rum/deref *el-ref)
fn-ready
(fn []
(set! (. viewer -currentScaleValue) "auto"))
fn-selection-ok
(fn [^js/MouseEvent e]
(let [^js/Selection selection (js/document.getSelection)
^js/Range sel-range (.getRangeAt selection 0)]
(cond
(.-isCollapsed selection)
(set-sel-state! {:collapsed true})
(and sel-range (.contains el (.-commonAncestorContainer sel-range)))
(set-sel-state! {:collapsed false :range sel-range :point {:x (.-clientX e) :y (.-clientY e)}}))))
fn-selection
(fn []
(let [*dirty (volatile! false)
fn-dirty #(vreset! *dirty true)]
(js/document.addEventListener "selectionchange" fn-dirty)
(js/document.addEventListener "mouseup"
(fn [^js e]
(and @*dirty (fn-selection-ok e))
(js/document.removeEventListener "selectionchange" fn-dirty))
#js {:once true})))]
(doto (.-eventBus viewer)
(.on "pagesinit" fn-ready))
(doto el
(.addEventListener "mousedown" fn-selection))
#(do
(doto (.-eventBus viewer)
(.off "pagesinit" fn-ready))
(doto el
(.removeEventListener "mousedown" fn-selection))))))
[(:viewer state)])
[:div.extensions__pdf-viewer {:ref *el-ref}
[:div.pdfViewer "viewer pdf"]]))
(rum/defc pdf-loader
[url]
(let [*doc-ref (rum/use-ref nil)
[state set-state!] (rum/use-state {:error nil :pdf-document nil :status nil})]
;; load
(rum/use-effect!
(fn []
(let [get-doc$ (fn [^js opts] (.-promise (js/pdfjsLib.getDocument opts)))
own-doc (rum/deref *doc-ref)
opts {:url url
:ownerDocument js/document
;;:cMapUrl "./js/pdfjs/cmaps/"
:cMapUrl "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.8.335/cmaps/"
:cMapPacked true}]
(p/finally
(p/catch (p/then
(do
(set-state! {:status :loading})
(get-doc$ (clj->js opts)))
#(do (js/console.log "+++" %)
(set-state! {:pdf-document %})))
#(set-state! {:error %}))
#(set-state! {:status :completed}))
#()))
[url])
[:div.extensions__pdf-loader {:ref *doc-ref}
(if (= (:status state) :loading)
[:h1 "Downloading PDF #" url]
(viewer (:pdf-document state)))
[:h3 (str (:error state))]]))
(rum/defc container
[]
(let [[prepared set-prepared!] (rum/use-state false)]
;; load assets
(rum/use-effect!
(fn []
(p/then
(pdf-utils/load-base-assets$)
(fn [] (set-prepared! true))))
[])
[:div.extensions__pdf-container.flex
(if prepared
(pdf-loader ACTIVE_FILE))]))
(rum/defc playground
[]
[:div.extensions__pdf-playground
(container)])

View File

@ -0,0 +1,81 @@
.extensions__pdf {
&-loader {
position: relative;
min-height: 90vh;
}
&-viewer {
position: absolute;
overflow: auto;
width: 100%;
height: 100%;
> .viewer-container {
}
}
}
body[data-page=home] {
#main-content-container {
margin: 0 !important;
padding: 0 !important;
}
.cp__sidebar-main-content {
> .pb-24 {
max-width: unset;
}
}
.extensions__pdf-loader {
width: 900px;
}
.extensions__pdf-container {
}
}
/* overrides for pdf_viewer.css from PDF.JS web viewer */
.textLayer {
z-index: 2;
opacity: 1;
mix-blend-mode: multiply;
}
.annotationLayer {
position: absolute;
top: 0;
z-index: 3;
}
.textLayer > div:not(.PdfHighlighter__highlight-layer):not(.Highlight):not(.Highlight-emoji) {
opacity: 1;
mix-blend-mode: multiply;
}
.textLayer ::selection {
background: rgba(252, 232, 151, 1);
mix-blend-mode: multiply;
}
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.textLayer {
opacity: 0.5;
}
}
/* Internet Explorer support method */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.textLayer {
opacity: 0.5
}
}
/* Microsoft Edge Browser 12+ (All) - @supports method */
@supports (-ms-ime-align:auto) {
.textLayer {
opacity: 0.5
}
}

View File

@ -0,0 +1,37 @@
(ns frontend.extensions.pdf.utils
(:require [promesa.core :as p]
[frontend.loader :refer [load]]))
(defn js-load$
[url]
(p/create
(fn [resolve]
(load url resolve))))
(defn load-base-assets$
[]
(p/let [_ (js-load$ "./static/js/pdfjs/pdf.js")
_ (js-load$ "./static/js/pdfjs/pdf_viewer.js")]))
(defn get-page-from-el
[^js/HTMLElement el]
(when-let [^js page-el (and el (.closest el ".page"))]
{:page-number (.. page-el -dataset -pageNumber)
:page-el page-el}))
(defn get-page-from-range
[^js/Range r]
(when-let [parent-el (and r (.. r -startContainer -parentElement))]
(get-page-from-el parent-el)))
(defn get-range-rects<-page-cnt
[^js/Range r ^js page-cnt]
(let [rge-rects (js->clj (.getClientRects r))
^js cnt-offset (.getBoundingClientRect page-cnt)]
(if (seq rge-rects)
(for [rect rge-rects]
{:top (- (+ (.-top rect) (.-scrollTop page-cnt)) (.-top cnt-offset))
:left (- (+ (.-left rect) (.-scrollLeft page-cnt)) (.-left cnt-offset))
:width (.-width rect)
:height (.-height rect)}))))

View File

@ -0,0 +1,580 @@
/* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.textLayer {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
overflow: hidden;
opacity: 0.2;
line-height: 1;
}
.textLayer > span {
color: transparent;
position: absolute;
white-space: pre;
cursor: text;
transform-origin: 0% 0%;
}
.textLayer .highlight {
margin: -1px;
padding: 1px;
background-color: rgba(180, 0, 170, 1);
border-radius: 4px;
}
.textLayer .highlight.begin {
border-radius: 4px 0 0 4px;
}
.textLayer .highlight.end {
border-radius: 0 4px 4px 0;
}
.textLayer .highlight.middle {
border-radius: 0;
}
.textLayer .highlight.selected {
background-color: rgba(0, 100, 0, 1);
}
.textLayer ::-moz-selection {
background: rgba(0, 0, 255, 1);
}
.textLayer ::selection {
background: rgba(0, 0, 255, 1);
}
.textLayer .endOfContent {
display: block;
position: absolute;
left: 0;
top: 100%;
right: 0;
bottom: 0;
z-index: -1;
cursor: default;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.textLayer .endOfContent.active {
top: 0;
}
.annotationLayer section {
position: absolute;
text-align: initial;
}
.annotationLayer .linkAnnotation > a,
.annotationLayer .buttonWidgetAnnotation.pushButton > a {
position: absolute;
font-size: 1em;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.annotationLayer .linkAnnotation > a:hover,
.annotationLayer .buttonWidgetAnnotation.pushButton > a:hover {
opacity: 0.2;
background: rgba(255, 255, 0, 1);
box-shadow: 0 2px 10px rgba(255, 255, 0, 1);
}
.annotationLayer .textAnnotation img {
position: absolute;
cursor: pointer;
}
.annotationLayer .textWidgetAnnotation input,
.annotationLayer .textWidgetAnnotation textarea,
.annotationLayer .choiceWidgetAnnotation select,
.annotationLayer .buttonWidgetAnnotation.checkBox input,
.annotationLayer .buttonWidgetAnnotation.radioButton input {
background-color: rgba(0, 54, 255, 0.13);
border: 1px solid transparent;
box-sizing: border-box;
font-size: 9px;
height: 100%;
margin: 0;
padding: 0 3px;
vertical-align: top;
width: 100%;
}
.annotationLayer .choiceWidgetAnnotation select option {
padding: 0;
}
.annotationLayer .buttonWidgetAnnotation.radioButton input {
border-radius: 50%;
}
.annotationLayer .textWidgetAnnotation textarea {
font: message-box;
font-size: 9px;
resize: none;
}
.annotationLayer .textWidgetAnnotation input[disabled],
.annotationLayer .textWidgetAnnotation textarea[disabled],
.annotationLayer .choiceWidgetAnnotation select[disabled],
.annotationLayer .buttonWidgetAnnotation.checkBox input[disabled],
.annotationLayer .buttonWidgetAnnotation.radioButton input[disabled] {
background: none;
border: 1px solid transparent;
cursor: not-allowed;
}
.annotationLayer .textWidgetAnnotation input:hover,
.annotationLayer .textWidgetAnnotation textarea:hover,
.annotationLayer .choiceWidgetAnnotation select:hover,
.annotationLayer .buttonWidgetAnnotation.checkBox input:hover,
.annotationLayer .buttonWidgetAnnotation.radioButton input:hover {
border: 1px solid rgba(0, 0, 0, 1);
}
.annotationLayer .textWidgetAnnotation input:focus,
.annotationLayer .textWidgetAnnotation textarea:focus,
.annotationLayer .choiceWidgetAnnotation select:focus {
background: none;
border: 1px solid transparent;
}
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:before,
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:after,
.annotationLayer .buttonWidgetAnnotation.radioButton input:checked:before {
background-color: rgba(0, 0, 0, 1);
content: "";
display: block;
position: absolute;
}
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:before,
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:after {
height: 80%;
left: 45%;
width: 1px;
}
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:before {
transform: rotate(45deg);
}
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:after {
transform: rotate(-45deg);
}
.annotationLayer .buttonWidgetAnnotation.radioButton input:checked:before {
border-radius: 50%;
height: 50%;
left: 30%;
top: 20%;
width: 50%;
}
.annotationLayer .textWidgetAnnotation input.comb {
font-family: monospace;
padding-left: 2px;
padding-right: 0;
}
.annotationLayer .textWidgetAnnotation input.comb:focus {
/*
* Letter spacing is placed on the right side of each character. Hence, the
* letter spacing of the last character may be placed outside the visible
* area, causing horizontal scrolling. We avoid this by extending the width
* when the element has focus and revert this when it loses focus.
*/
width: 115%;
}
.annotationLayer .buttonWidgetAnnotation.checkBox input,
.annotationLayer .buttonWidgetAnnotation.radioButton input {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
padding: 0;
}
.annotationLayer .popupWrapper {
position: absolute;
width: 20em;
}
.annotationLayer .popup {
position: absolute;
z-index: 200;
max-width: 20em;
background-color: rgba(255, 255, 153, 1);
box-shadow: 0 2px 5px rgba(136, 136, 136, 1);
border-radius: 2px;
padding: 6px;
margin-left: 5px;
cursor: pointer;
font: message-box;
font-size: 9px;
white-space: normal;
word-wrap: break-word;
}
.annotationLayer .popup > * {
font-size: 9px;
}
.annotationLayer .popup h1 {
display: inline-block;
}
.annotationLayer .popup span {
display: inline-block;
margin-left: 5px;
}
.annotationLayer .popup p {
border-top: 1px solid rgba(51, 51, 51, 1);
margin-top: 2px;
padding-top: 2px;
}
.annotationLayer .highlightAnnotation,
.annotationLayer .underlineAnnotation,
.annotationLayer .squigglyAnnotation,
.annotationLayer .strikeoutAnnotation,
.annotationLayer .freeTextAnnotation,
.annotationLayer .lineAnnotation svg line,
.annotationLayer .squareAnnotation svg rect,
.annotationLayer .circleAnnotation svg ellipse,
.annotationLayer .polylineAnnotation svg polyline,
.annotationLayer .polygonAnnotation svg polygon,
.annotationLayer .caretAnnotation,
.annotationLayer .inkAnnotation svg polyline,
.annotationLayer .stampAnnotation,
.annotationLayer .fileAttachmentAnnotation {
cursor: pointer;
}
* /* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
.xfaLayer {
position: absolute;
top: 0;
left: 0;
z-index: 200;
transform-origin: 0 0;
}
.xfaLayer * {
color: inherit;
font: inherit;
-webkit-font-kerning: inherit;
font-kerning: inherit;
letter-spacing: inherit;
text-align: inherit;
text-decoration: inherit;
vertical-align: inherit;
box-sizing: border-box;
}
.xfaFont {
color: black;
font-weight: normal;
-webkit-font-kerning: none;
font-kerning: none;
font-size: 10px;
font-style: normal;
letter-spacing: 0;
text-decoration: none;
vertical-align: 0;
}
.xfaDraw {
z-index: 200;
}
.xfaExclgroup {
z-index: 300;
}
.xfaField {
z-index: 300;
}
.xfaSubform {
z-index: 100;
}
.xfaLabel {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
height: 100%;
}
.xfaCaption {
flex: 1 1 auto;
}
.xfaTextfield,
.xfaSelect {
width: 100%;
height: 100%;
flex: 1 1 auto;
border: none;
}
.xfaLabel > input[type="checkbox"] {
/* Use this trick to make the checkbox invisible but
but still focusable. */
position: absolute;
left: -99999px;
}
.xfaLabel > input[type="checkbox"]:focus + .xfaCheckboxMark {
box-shadow: 0 0 5px rgba(0, 0, 0, 0.7);
}
.xfaCheckboxMark {
cursor: pointer;
flex: 0 0 auto;
border-style: solid;
border-width: 2px;
border-color: #8f8f9d;
font-size: 10px;
line-height: 10px;
width: 10px;
height: 10px;
text-align: center;
vertical-align: middle;
display: flex;
flex-direction: row;
align-items: center;
}
.xfaCheckbox:checked + .xfaCheckboxMark::after {
content: attr(mark);
}
.xfaButton {
cursor: pointer;
width: 100%;
height: 100%;
border: none;
text-align: center;
}
.xfaButton:hover {
background: Highlight;
}
.xfaLrTb,
.xfaRlTb,
.xfaTb,
.xfaPosition {
display: block;
}
.xfaPosition {
position: relative;
}
.xfaValignMiddle {
display: flex;
align-items: center;
}
.xfaLrTb > div {
display: inline;
float: left;
}
.xfaRlTb > div {
display: inline;
float: right;
}
.xfaTable {
display: flex;
flex-direction: column;
}
.xfaTable .xfaRow {
display: flex;
flex-direction: row;
flex: 1 1 auto;
}
.xfaTable .xfaRow > div {
flex: 1 1 auto;
}
.xfaTable .xfaRlRow {
display: flex;
flex-direction: row-reverse;
flex: 1;
}
.xfaTable .xfaRlRow > div {
flex: 1;
}
.pdfViewer .canvasWrapper {
overflow: hidden;
}
.pdfViewer .page {
direction: ltr;
width: 816px;
height: 1056px;
margin: 1px auto -8px;
position: relative;
overflow: visible;
border: 9px solid transparent;
background-clip: content-box;
-o-border-image: url(images/shadow.png) 9 9 repeat;
border-image: url(images/shadow.png) 9 9 repeat;
background-color: rgba(255, 255, 255, 1);
}
.pdfViewer.removePageBorders .page {
margin: 0 auto 10px;
border: none;
}
.pdfViewer.singlePageView {
display: inline-block;
}
.pdfViewer.singlePageView .page {
margin: 0;
border: none;
}
.pdfViewer.scrollHorizontal,
.pdfViewer.scrollWrapped,
.spread {
margin-left: 3.5px;
margin-right: 3.5px;
text-align: center;
}
.pdfViewer.scrollHorizontal,
.spread {
white-space: nowrap;
}
.pdfViewer.removePageBorders,
.pdfViewer.scrollHorizontal .spread,
.pdfViewer.scrollWrapped .spread {
margin-left: 0;
margin-right: 0;
}
.spread .page,
.pdfViewer.scrollHorizontal .page,
.pdfViewer.scrollWrapped .page,
.pdfViewer.scrollHorizontal .spread,
.pdfViewer.scrollWrapped .spread {
display: inline-block;
vertical-align: middle;
}
.spread .page,
.pdfViewer.scrollHorizontal .page,
.pdfViewer.scrollWrapped .page {
margin-left: -3.5px;
margin-right: -3.5px;
}
.pdfViewer.removePageBorders .spread .page,
.pdfViewer.removePageBorders.scrollHorizontal .page,
.pdfViewer.removePageBorders.scrollWrapped .page {
margin-left: 5px;
margin-right: 5px;
}
.pdfViewer .page canvas {
margin: 0;
display: block;
}
.pdfViewer .page canvas[hidden] {
display: none;
}
.pdfViewer .page .loadingIcon {
position: absolute;
display: block;
left: 0;
top: 0;
right: 0;
bottom: 0;
background: url("images/loading-icon.gif") center no-repeat;
}
.pdfPresentationMode .pdfViewer {
margin-left: 0;
margin-right: 0;
}
.pdfPresentationMode .pdfViewer .page,
.pdfPresentationMode .pdfViewer .spread {
display: block;
}
.pdfPresentationMode .pdfViewer .page,
.pdfPresentationMode .pdfViewer.removePageBorders .page {
margin-left: auto;
margin-right: auto;
}
.pdfPresentationMode:-webkit-full-screen .pdfViewer .page {
margin-bottom: 100%;
border: 0;
}
.pdfPresentationMode:-moz-full-screen .pdfViewer .page {
margin-bottom: 100%;
border: 0;
}
.pdfPresentationMode:fullscreen .pdfViewer .page {
margin-bottom: 100%;
border: 0;
}