aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Layout.mjs
blob: 23588a1cf3c67bd6d47b95bc2037787ba6c82f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import PlainDraggable from 'plain-draggable'
import { onRemove, animateRectTransition } from './utils'

export class Layout {
  constructor(options = {}) {
    if (!options.name) throw Error("Layout name is not given")
    this.name = options.name
    this.enterHandler = options.enterHandler
    this.leaveHandler = options.leaveHandler
  }
  valueOf = () => this.name
}

export class SideBySide extends Layout {
  name = "side-by-side"

  enterHandler = ({ container, htmlHolder, showcase }) => {
    const bar = document.createElement('div')
    bar.className = 'bar'
    bar.innerHTML = '<div class="bar-handle"></div>'
    const handle = bar.querySelector('.bar-handle')
    container.appendChild(bar)

    // Resize views by value
    const resizeByLeft = (left) => {
      htmlHolder.style.width = (left) + "px"
      showcase.style.width = (parseFloat(getComputedStyle(container).width) - left) + "px"
    }

    const draggable = new PlainDraggable(bar, {
      handle: handle,
      containment: { left: '25%', top: 0, right: '75%', height: 0 },
    })
    draggable.draggableCursor = "grab"

    draggable.onDrag = (pos) => {
      handle.style.transform = 'unset'
      resizeByLeft(pos.left)
    }
    draggable.onDragEnd = (_) => {
      handle.removeAttribute('style')
    }

    onRemove(bar, () => draggable.remove())
  }

  leaveHandler = ({ container }) => {
    container.querySelector('.bar')?.remove()
  }
}

export class Overlay extends Layout {
  name = "overlay"

  saveLeftTopAsData = (element) => {
    const { left, top } = element.getBoundingClientRect()
    element.setAttribute('data-left', left)
    element.setAttribute('data-top', top)
  }

  addDraggable = (element) => {
    // Make sure current element always on top
    const siblings = Array.from(element.parentElement?.querySelectorAll(':scope > *') ?? [])
    element.onmouseover = () => {
      siblings.forEach(e => e.style.removeProperty('z-index'))
      element.style.zIndex = '9000'
    }

    // Add draggable part
    const draggablePart = document.createElement('div')
    element.appendChild(draggablePart)
    draggablePart.className = 'draggable-part'
    draggablePart.innerHTML = '<div class="handle">\u2630</div>'
    draggablePart.title = 'Use middle-click to remove block'
    draggablePart.onmouseup = (e) => {
      // Hide block with middle click
      if (e.button === 1) {
        element.classList.add('hide')
      }
    }

    // Add draggable instance
    const { left, top } = element.getBoundingClientRect()
    const draggable = new PlainDraggable(element, {
      top: top,
      left: left,
      handle: draggablePart,
      snap: { x: { step: 20 }, y: { step: 20 } },
    })

    // FIXME use pure CSS to hide utils
    const utils = element.querySelector('.utils')
    draggable.onDragStart = () => {
      utils.style.display = 'none'
      element.classList.add('drag')
    }

    draggable.onDragEnd = () => {
      utils.style = ''
      element.classList.remove('drag')
      element.style.zIndex = '9000'
    }

    // Reposition draggable instance when resized
    new ResizeObserver(() => {
      try {
        draggable.position();
      } catch (_) {
        null
      }
    }).observe(element);

    // Callback for remove
    onRemove(element, () => {
      draggable.remove()
    })
  }

  enterHandler = ({ htmlHolder, blocks }) => {

    // FIXME It is weird rect from this method and this scope are different...
    blocks.forEach(this.saveLeftTopAsData)

    // Create draggable blocks and set each position by previous one
    let [left, top] = [20, 20]
    blocks.forEach(block => {
      const originLeft = Number(block.getAttribute('data-left'))
      const originTop = Number(block.getAttribute('data-top'))

      // Create draggable block
      const wrapper = document.createElement('div')
      wrapper.classList.add('draggable-block')
      wrapper.innerHTML = `
        <div class="utils">
          <div id="close">\u274C</div>
          <div id="plus-font-size" ">\u2795</div>
          <div id="minus-font-size">\u2796</div>
        </div>
      `

      wrapper.appendChild(block)
      htmlHolder.appendChild(wrapper)
      wrapper.style.left = left + "px"
      wrapper.style.top = top + "px"
      const { width } = wrapper.getBoundingClientRect()
      left += width + 30
      if (left > window.innerWidth) {
        top += 200
        left = left % window.innerWidth
      }

      animateRectTransition(
        wrapper,
        { left: originLeft, top: originTop },
        { resume: true, duration: 500 }
      )
        .finished
        .finally(() => this.addDraggable(wrapper))

      // Trivial case:
      // This hack make sure utils remains at the same place even when wrapper resized
      // Prevent DOMRect changes when user clicking plus/minus button many times
      const utils = wrapper.querySelector('.utils')
      utils.onmouseover = () => {
        const { left, top } = utils.getBoundingClientRect()
        utils.style.cssText = `visibility: visible; z-index: 9000; position: fixed; transition: unset; left: ${left}px; top: ${top}px;`
        document.body.appendChild(utils)
      }
      utils.onmouseout = () => {
        wrapper.appendChild(utils)
        utils.removeAttribute('style')
      }

      // Close button
      wrapper.querySelector('#close').onclick = () => {
        wrapper.classList.add('hide')
        utils.removeAttribute('style')
      }
      // Plus/Minus font-size of content
      wrapper.querySelector('#plus-font-size').onclick = () => {
        const fontSize = parseFloat(getComputedStyle(block).fontSize) / 16
        block.style.fontSize = `${fontSize + 0.2}rem`
      }
      wrapper.querySelector('#minus-font-size').onclick = () => {
        const fontSize = parseFloat(getComputedStyle(block).fontSize) / 16
        block.style.fontSize = `${fontSize - 0.2}rem`
      }
    })
  }

  leaveHandler = ({ htmlHolder, blocks }) => {
    const resumeFromDraggable = (block) => {
      const draggableContainer = block.closest('.draggable-block')
      if (!draggableContainer) return
      htmlHolder.appendChild(block)
      draggableContainer.remove()
    }
    blocks.forEach(resumeFromDraggable)
  }
}