Pārlūkot izejas kodu

:art: fix https://github.com/siyuan-note/siyuan/issues/5713

Vanessa 2 gadi atpakaļ
vecāks
revīzija
092035c564
1 mainītis faili ar 51 papildinājumiem un 23 dzēšanām
  1. 51 23
      app/src/protyle/header/Background.ts

+ 51 - 23
app/src/protyle/header/Background.ts

@@ -49,31 +49,59 @@ export class Background {
         this.tagsElement = this.element.querySelector(".protyle-background__tags") as HTMLElement;
         this.iconElement = this.element.querySelector(".protyle-background__icon") as HTMLElement;
         this.imgElement = this.element.firstElementChild.firstElementChild as HTMLImageElement;
-        this.imgElement.addEventListener("mousedown", (event: MouseEvent & { target: HTMLElement }) => {
-            event.preventDefault();
-            if (!this.element.firstElementChild.querySelector(".protyle-icons").classList.contains("fn__none")) {
-                return;
-            }
-            const y = event.clientY;
-            const documentSelf = document;
-            const height = this.imgElement.naturalHeight * this.imgElement.clientWidth / this.imgElement.naturalWidth - this.imgElement.clientHeight;
-            let originalPositionY = parseFloat(this.imgElement.style.objectPosition.substring(7)) || 50;
-            if (this.imgElement.style.objectPosition.endsWith("px")) {
-                originalPositionY = -parseInt(this.imgElement.style.objectPosition.substring(7)) / height * 100;
-            }
-            documentSelf.onmousemove = (moveEvent: MouseEvent) => {
-                this.imgElement.style.objectPosition = `center ${((y - moveEvent.clientY) / height * 100 + originalPositionY).toFixed(2)}%`;
+        if (isMobile()) {
+            this.imgElement.addEventListener("touchstart", (event: TouchEvent & { target: HTMLElement }) => {
                 event.preventDefault();
-            };
+                if (!this.element.firstElementChild.querySelector(".protyle-icons").classList.contains("fn__none")) {
+                    return;
+                }
+                const y = event.touches[0].clientY;
+                const documentSelf = document;
+                const height = this.imgElement.naturalHeight * this.imgElement.clientWidth / this.imgElement.naturalWidth - this.imgElement.clientHeight;
+                let originalPositionY = parseFloat(this.imgElement.style.objectPosition.substring(7)) || 50;
+                if (this.imgElement.style.objectPosition.endsWith("px")) {
+                    originalPositionY = -parseInt(this.imgElement.style.objectPosition.substring(7)) / height * 100;
+                }
+                documentSelf.ontouchmove = (moveEvent) => {
+                    this.imgElement.style.objectPosition = `center ${((y - moveEvent.touches[0].clientY) / height * 100 + originalPositionY).toFixed(2)}%`;
+                    event.preventDefault();
+                };
 
-            documentSelf.onmouseup = () => {
-                documentSelf.onmousemove = null;
-                documentSelf.onmouseup = null;
-                documentSelf.ondragstart = null;
-                documentSelf.onselectstart = null;
-                documentSelf.onselect = null;
-            };
-        });
+                documentSelf.ontouchend = () => {
+                    documentSelf.ontouchmove = null;
+                    documentSelf.ontouchstart = null;
+                    documentSelf.ondragstart = null;
+                    documentSelf.onselectstart = null;
+                    documentSelf.onselect = null;
+                };
+            });
+        } else {
+            this.imgElement.addEventListener("mousedown", (event: MouseEvent & { target: HTMLElement }) => {
+                event.preventDefault();
+                if (!this.element.firstElementChild.querySelector(".protyle-icons").classList.contains("fn__none")) {
+                    return;
+                }
+                const y = event.clientY;
+                const documentSelf = document;
+                const height = this.imgElement.naturalHeight * this.imgElement.clientWidth / this.imgElement.naturalWidth - this.imgElement.clientHeight;
+                let originalPositionY = parseFloat(this.imgElement.style.objectPosition.substring(7)) || 50;
+                if (this.imgElement.style.objectPosition.endsWith("px")) {
+                    originalPositionY = -parseInt(this.imgElement.style.objectPosition.substring(7)) / height * 100;
+                }
+                documentSelf.onmousemove = (moveEvent: MouseEvent) => {
+                    this.imgElement.style.objectPosition = `center ${((y - moveEvent.clientY) / height * 100 + originalPositionY).toFixed(2)}%`;
+                    event.preventDefault();
+                };
+
+                documentSelf.onmouseup = () => {
+                    documentSelf.onmousemove = null;
+                    documentSelf.onmouseup = null;
+                    documentSelf.ondragstart = null;
+                    documentSelf.onselectstart = null;
+                    documentSelf.onselect = null;
+                };
+            });
+        }
         this.element.querySelector("input").addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => {
             if (event.target.files.length === 0) {
                 return;