|
10 | 10 | import { clickoutside } from "@svelte-put/clickoutside"
|
11 | 11 | import { Appearance } from "$lib/enums"
|
12 | 12 | import type { ContextItem } from "$lib/types"
|
13 |
| - import { createEventDispatcher, onMount, tick } from "svelte" |
| 13 | + import { createEventDispatcher, onDestroy, onMount, tick } from "svelte" |
14 | 14 | import { log } from "$lib/utils/Logger"
|
15 | 15 | import type { PluginListenerHandle } from "@capacitor/core"
|
16 | 16 | import { isAndroidOriOS } from "$lib/utils/Mobile"
|
17 | 17 |
|
18 | 18 | let visible: boolean = false
|
19 | 19 | let coords: [number, number] = [0, 0]
|
20 | 20 | let context: HTMLElement
|
| 21 | + let slotContainer: HTMLElement |
21 | 22 | export let items: ContextItem[] = []
|
22 | 23 | export let hook: string = ""
|
23 | 24 |
|
24 | 25 | const dispatch = createEventDispatcher()
|
25 | 26 |
|
26 | 27 | function onClose(event: CustomEvent<MouseEvent> | MouseEvent) {
|
| 28 | + if (isLongPress) { |
| 29 | + return |
| 30 | + } |
27 | 31 | visible = false
|
28 | 32 | dispatch("close", event)
|
29 | 33 | close_context = undefined
|
|
34 | 38 | const { width, height } = context.getBoundingClientRect()
|
35 | 39 |
|
36 | 40 | const offsetX = evt.pageX
|
37 |
| - const offsetY = evt.pageY - keyboardHeight / 2.5 |
| 41 | + const offsetY = evt.pageY |
38 | 42 | const screenWidth = evt.view!.innerWidth
|
39 |
| - const screenHeight = evt.view!.innerHeight |
| 43 | + const screenHeight = evt.view!.innerHeight - keyboardHeight |
40 | 44 |
|
41 | 45 | const overFlowX = screenWidth < width + offsetX
|
42 | 46 | const overFlowY = screenHeight < height + offsetY
|
|
54 | 58 | if (close_context !== undefined) {
|
55 | 59 | close_context()
|
56 | 60 | }
|
57 |
| - close_context = () => (visible = false) |
| 61 | + close_context = () => { |
| 62 | + if (!isLongPress) { |
| 63 | + visible = false |
| 64 | + } |
| 65 | + } |
58 | 66 | evt.preventDefault()
|
59 |
| - visible = true |
60 | 67 | coords = [evt.clientX, evt.clientY]
|
| 68 | + visible = true |
61 | 69 | await tick()
|
62 | 70 | coords = calculatePos(evt)
|
63 | 71 | }
|
| 72 | +
|
64 | 73 | let keyboardHeight = 0
|
65 | 74 | onMount(() => {
|
66 | 75 | let mobileKeyboardListener01: PluginListenerHandle | undefined
|
|
85 | 94 | }
|
86 | 95 | })
|
87 | 96 |
|
| 97 | + let touchTimer: number | undefined |
| 98 | + let isLongPress: boolean = false |
| 99 | +
|
| 100 | + function handleTouchStart(evt: TouchEvent) { |
| 101 | + if (evt.touches.length === 1) { |
| 102 | + isLongPress = false |
| 103 | + let longPressElement = evt.target as HTMLElement |
| 104 | + longPressElement.style.pointerEvents = "none" |
| 105 | + touchTimer = window.setTimeout(() => { |
| 106 | + const touch = evt.touches[0] |
| 107 | + const mouseEvent = new MouseEvent("contextmenu", { |
| 108 | + bubbles: true, |
| 109 | + cancelable: true, |
| 110 | + view: window, |
| 111 | + clientX: touch.clientX, |
| 112 | + clientY: touch.clientY, |
| 113 | + }) |
| 114 | + isLongPress = true |
| 115 | + openContext(mouseEvent) |
| 116 | + }, 500) |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + function handleTouchEnd(evt: TouchEvent) { |
| 121 | + clearTimeout(touchTimer) |
| 122 | + let longPressElement = evt.target as HTMLElement |
| 123 | + longPressElement.style.pointerEvents = "" |
| 124 | + if (isLongPress) { |
| 125 | + evt.preventDefault() |
| 126 | + } |
| 127 | + setTimeout(() => { |
| 128 | + isLongPress = false |
| 129 | + }, 100) |
| 130 | + } |
| 131 | +
|
| 132 | + function handleTouchMove(evt: TouchEvent) { |
| 133 | + clearTimeout(touchTimer) |
| 134 | + isLongPress = false |
| 135 | + } |
| 136 | +
|
88 | 137 | function handleItemClick(e: MouseEvent, item: ContextItem) {
|
89 | 138 | e.stopPropagation()
|
90 | 139 | log.info(`Clicked ${item.text}`)
|
|
94 | 143 | })
|
95 | 144 | onClose(customEvent)
|
96 | 145 | }
|
| 146 | +
|
| 147 | + onMount(() => { |
| 148 | + slotContainer.addEventListener("touchstart", handleTouchStart) |
| 149 | + slotContainer.addEventListener("touchend", handleTouchEnd) |
| 150 | + slotContainer.addEventListener("touchmove", handleTouchMove) |
| 151 | + }) |
| 152 | +
|
| 153 | + onDestroy(() => { |
| 154 | + slotContainer.removeEventListener("touchstart", handleTouchStart) |
| 155 | + slotContainer.removeEventListener("touchend", handleTouchEnd) |
| 156 | + slotContainer.removeEventListener("touchmove", handleTouchMove) |
| 157 | + }) |
97 | 158 | </script>
|
98 | 159 |
|
99 |
| -<slot name="content" open={openContext} /> |
| 160 | +<div class="long-press-div-container" bind:this={slotContainer}> |
| 161 | + <slot name="content" open={openContext} /> |
| 162 | +</div> |
100 | 163 | {#if visible}
|
101 | 164 | <div id="context-menu" data-cy={hook} bind:this={context} use:clickoutside on:clickoutside={onClose} style={`position: fixed; left: ${coords[0]}px; top: ${coords[1]}px;`}>
|
102 | 165 | <slot name="items" close={onClose}></slot>
|
|
115 | 178 | {/if}
|
116 | 179 |
|
117 | 180 | <style lang="scss">
|
| 181 | + .long-press-div-container { |
| 182 | + all: unset; |
| 183 | + display: contents; |
| 184 | + -webkit-touch-callout: none !important; |
| 185 | + -webkit-user-select: none !important; |
| 186 | + user-select: none; |
| 187 | + touch-action: none; |
| 188 | + } |
| 189 | +
|
118 | 190 | #context-menu {
|
119 | 191 | z-index: 1000;
|
120 | 192 | position: fixed;
|
|
0 commit comments