From f80f8d748b2c06c1458b18302d6e3e7f02af05d8 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 22 Sep 2020 11:48:35 +0200 Subject: [PATCH 1/5] Use flex for modal --- index.html | 1 + src/index.js | 4 +--- style.css | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 12d3a60..c6c9a62 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@ +
diff --git a/src/index.js b/src/index.js index ef6aa1b..cba1b79 100644 --- a/src/index.js +++ b/src/index.js @@ -49,9 +49,7 @@ const dispatch = target => { window.onpopstate = e => { dispatch(document.location.search) } -const target = document.createElement('div') -display = new Display(dispatch, target) +display = new Display(dispatch, document.body.firstChild) if (document.location.search) dispatch(document.location.search) else display.renderIntro(intro) -document.body.appendChild(target) diff --git a/style.css b/style.css index eb63a20..8ce632a 100644 --- a/style.css +++ b/style.css @@ -75,19 +75,19 @@ body { left: 0; top: 0; background-color: #e1eef280; - padding: 1em; box-sizing: border-box; cursor: default; + display: flex; + align-items: center; + justify-content: center; +} +a.fullview { + text-decoration: none; } .fullview > div { - position: fixed; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - min-width: 20em; + font-size: 1.2em; max-height: 100%; - max-width: 30em; - overflow: auto; + max-width: 75vh; background-color: #006680; padding: 1em; color: white; @@ -96,7 +96,6 @@ body { box-sizing: border-box; display: flex; flex-direction: column; - font-size: 1.2em; } .fullview > div img { float: right; @@ -118,6 +117,7 @@ h1, .desc { border: 3px solid #1a1c1c; color: #1a1c1c; padding: 0 0.5em; + overflow: auto; } .fulltext p { @@ -130,7 +130,7 @@ blockquote { padding: .5em; quotes: "\201C""\201D""\2018""\2019"; /* Otherwise the :before is larger than the blockquote and creates a scrollbar in the parent */ - min-height: 3em; + min-height: 4em; } blockquote:before { color: #1a1c1c66; From 253559bfc8a8c588ccebc0d521a915c5f42d34fd Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 22 Sep 2020 11:52:21 +0200 Subject: [PATCH 2/5] use flex for body --- index.html | 2 +- style.css | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index c6c9a62..067da55 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ -
+
diff --git a/style.css b/style.css index 8ce632a..0e15793 100644 --- a/style.css +++ b/style.css @@ -8,22 +8,27 @@ src: url("/IMFePIsc29P.ttf"); } - body { background-color: #e1eef2; margin: 0; font-family: "Fell"; hyphens: auto; + display: flex; + flex-direction: column; + height: 100%; + width: 100%; } .wrapper { max-width: 70em; + width: 100%; height: 100%; margin: 0 auto; background-color: #FFC63F; position: relative; box-sizing: border-box; padding: 20px; + flex: 1; } .items { From 4495444ec928434ef7c98aa2331c14e00b7aefd5 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 25 Sep 2020 19:58:48 +0200 Subject: [PATCH 3/5] Make it work on mobile devices --- index.html | 1 + src/display.js | 9 +++++---- style.css | 15 ++++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 067da55..0f1ba70 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,6 @@ + diff --git a/src/display.js b/src/display.js index 5ea43c6..2b3b38e 100644 --- a/src/display.js +++ b/src/display.js @@ -78,10 +78,11 @@ export class Display { let currentBB = this.layout.getBoundingBox() let width = this.target.scrollWidth || 100 let height = this.target.scrollHeight || 100 + const ballRadius = Math.min(document.body.clientWidth, document.body.clientHeight) * 0.06 var toScreen = function(p) { var size = currentBB.topright.subtract(currentBB.bottomleft); - var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * (width - 60) + 30; - var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - 60) + 30; + var sx = p.subtract(currentBB.bottomleft).divide(size.x).x * (width - ballRadius * 2) + ballRadius; + var sy = p.subtract(currentBB.bottomleft).divide(size.y).y * (height - ballRadius * 2) + ballRadius; return new Springy.Vector(sx, sy); }; @@ -133,8 +134,8 @@ export class Display { } dom.className = item.data.state dom.style.position = 'absolute' - dom.style.left = (target.x - 60) + 'px' - dom.style.top = (target.y - 60) + 'px' + dom.style.left = (target.x - ballRadius) + 'px' + dom.style.top = (target.y - ballRadius) + 'px' dom.appendChild(a) field.appendChild(dom) }, undefined, undefined, diff --git a/style.css b/style.css index 0e15793..b21099e 100644 --- a/style.css +++ b/style.css @@ -27,7 +27,7 @@ body { background-color: #FFC63F; position: relative; box-sizing: border-box; - padding: 20px; + padding: 1em; flex: 1; } @@ -42,10 +42,10 @@ body { } .items > li a { - width: 120px; - height: 120px; + width: min(12vh, 12vw); + height: min(12vh, 12vw); background: #D66C00; - border-radius: 80px; + border-radius: min(7vh, 7vw); display: block; border: 3px solid #1a1c1c; } @@ -69,7 +69,7 @@ body { position: absolute; transform-origin: 0 0; font-size: 1.2em; - padding: 0 55px; + padding: 0 min(6vh, 6vw); box-sizing: border-box; } @@ -104,8 +104,9 @@ a.fullview { } .fullview > div img { float: right; - width: 120px; - border-radius: 80px; + width: 7em; + height: 7em; + border-radius: 7em; border: 3px solid #1a1c1c; margin-bottom: 1em; } From 619115941499374c82a7c8096fe948a3e1ccce37 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 25 Sep 2020 20:12:40 +0200 Subject: [PATCH 4/5] Nice scrollbar --- src/display.js | 4 ++-- style.css | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/display.js b/src/display.js index 2b3b38e..09ee85a 100644 --- a/src/display.js +++ b/src/display.js @@ -159,8 +159,8 @@ export class Display {

${name}

${desc}

-
${text} -
${data.quote || ''}
+
${text} +
${data.quote || ''}
`) target.appendChild(wrapper) } diff --git a/style.css b/style.css index b21099e..9b79ded 100644 --- a/style.css +++ b/style.css @@ -116,14 +116,21 @@ h1, .desc { margin-left: 0.5rem; } -.fulltext { +/* This wrapper is necessary so that the scrollbar doesn't clip out of + * the rounded border */ +.fulltext-wrapper { flex-grow: 1; background-color: #99d7e9; border-radius: 20px; border: 3px solid #1a1c1c; color: #1a1c1c; + overflow: hidden; +} + +.fulltext { padding: 0 0.5em; - overflow: auto; + overflow-y: auto; + height: 100%; } .fulltext p { From d4c49ed8f032d64eaac7c324e089593d369d4988 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 25 Sep 2020 20:28:50 +0200 Subject: [PATCH 5/5] Make connections work on mobile devices --- DragDropTouch.js | 447 +++++++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + src/display.js | 4 +- style.css | 7 +- 4 files changed, 457 insertions(+), 2 deletions(-) create mode 100644 DragDropTouch.js diff --git a/DragDropTouch.js b/DragDropTouch.js new file mode 100644 index 0000000..107825c --- /dev/null +++ b/DragDropTouch.js @@ -0,0 +1,447 @@ +var DragDropTouch; +(function (DragDropTouch_1) { + 'use strict'; + /** + * Object used to hold the data that is being dragged during drag and drop operations. + * + * It may hold one or more data items of different types. For more information about + * drag and drop operations and data transfer objects, see + * HTML Drag and Drop API. + * + * This object is created automatically by the @see:DragDropTouch singleton and is + * accessible through the @see:dataTransfer property of all drag events. + */ + var DataTransfer = (function () { + function DataTransfer() { + this._dropEffect = 'move'; + this._effectAllowed = 'all'; + this._data = {}; + } + Object.defineProperty(DataTransfer.prototype, "dropEffect", { + /** + * Gets or sets the type of drag-and-drop operation currently selected. + * The value must be 'none', 'copy', 'link', or 'move'. + */ + get: function () { + return this._dropEffect; + }, + set: function (value) { + this._dropEffect = value; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(DataTransfer.prototype, "effectAllowed", { + /** + * Gets or sets the types of operations that are possible. + * Must be one of 'none', 'copy', 'copyLink', 'copyMove', 'link', + * 'linkMove', 'move', 'all' or 'uninitialized'. + */ + get: function () { + return this._effectAllowed; + }, + set: function (value) { + this._effectAllowed = value; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(DataTransfer.prototype, "types", { + /** + * Gets an array of strings giving the formats that were set in the @see:dragstart event. + */ + get: function () { + return Object.keys(this._data); + }, + enumerable: true, + configurable: true + }); + /** + * Removes the data associated with a given type. + * + * The type argument is optional. If the type is empty or not specified, the data + * associated with all types is removed. If data for the specified type does not exist, + * or the data transfer contains no data, this method will have no effect. + * + * @param type Type of data to remove. + */ + DataTransfer.prototype.clearData = function (type) { + if (type != null) { + delete this._data[type]; + } + else { + this._data = null; + } + }; + /** + * Retrieves the data for a given type, or an empty string if data for that type does + * not exist or the data transfer contains no data. + * + * @param type Type of data to retrieve. + */ + DataTransfer.prototype.getData = function (type) { + return this._data[type] || ''; + }; + /** + * Set the data for a given type. + * + * For a list of recommended drag types, please see + * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Recommended_Drag_Types. + * + * @param type Type of data to add. + * @param value Data to add. + */ + DataTransfer.prototype.setData = function (type, value) { + this._data[type] = value; + }; + /** + * Set the image to be used for dragging if a custom one is desired. + * + * @param img An image element to use as the drag feedback image. + * @param offsetX The horizontal offset within the image. + * @param offsetY The vertical offset within the image. + */ + DataTransfer.prototype.setDragImage = function (img, offsetX, offsetY) { + var ddt = DragDropTouch._instance; + ddt._imgCustom = img; + ddt._imgOffset = { x: offsetX, y: offsetY }; + }; + return DataTransfer; + }()); + DragDropTouch_1.DataTransfer = DataTransfer; + /** + * Defines a class that adds support for touch-based HTML5 drag/drop operations. + * + * The @see:DragDropTouch class listens to touch events and raises the + * appropriate HTML5 drag/drop events as if the events had been caused + * by mouse actions. + * + * The purpose of this class is to enable using existing, standard HTML5 + * drag/drop code on mobile devices running IOS or Android. + * + * To use, include the DragDropTouch.js file on the page. The class will + * automatically start monitoring touch events and will raise the HTML5 + * drag drop events (dragstart, dragenter, dragleave, drop, dragend) which + * should be handled by the application. + * + * For details and examples on HTML drag and drop, see + * https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Drag_operations. + */ + var DragDropTouch = (function () { + /** + * Initializes the single instance of the @see:DragDropTouch class. + */ + function DragDropTouch() { + this._lastClick = 0; + // enforce singleton pattern + if (DragDropTouch._instance) { + throw 'DragDropTouch instance already created.'; + } + // detect passive event support + // https://github.com/Modernizr/Modernizr/issues/1894 + var supportsPassive = false; + document.addEventListener('test', function () { }, { + get passive() { + supportsPassive = true; + return true; + } + }); + // listen to touch events + if ('ontouchstart' in document) { + var d = document, ts = this._touchstart.bind(this), tm = this._touchmove.bind(this), te = this._touchend.bind(this), opt = supportsPassive ? { passive: false, capture: false } : false; + d.addEventListener('touchstart', ts, opt); + d.addEventListener('touchmove', tm, opt); + d.addEventListener('touchend', te); + d.addEventListener('touchcancel', te); + } + } + /** + * Gets a reference to the @see:DragDropTouch singleton. + */ + DragDropTouch.getInstance = function () { + return DragDropTouch._instance; + }; + // ** event handlers + DragDropTouch.prototype._touchstart = function (e) { + var _this = this; + if (this._shouldHandle(e)) { + // raise double-click and prevent zooming + if (Date.now() - this._lastClick < DragDropTouch._DBLCLICK) { + if (this._dispatchEvent(e, 'dblclick', e.target)) { + e.preventDefault(); + this._reset(); + return; + } + } + // clear all variables + this._reset(); + // get nearest draggable element + var src = this._closestDraggable(e.target); + if (src) { + // give caller a chance to handle the hover/move events + if (!this._dispatchEvent(e, 'mousemove', e.target) && + !this._dispatchEvent(e, 'mousedown', e.target)) { + // get ready to start dragging + this._dragSource = src; + this._ptDown = this._getPoint(e); + this._lastTouch = e; + e.preventDefault(); + // show context menu if the user hasn't started dragging after a while + setTimeout(function () { + if (_this._dragSource == src && _this._img == null) { + if (_this._dispatchEvent(e, 'contextmenu', src)) { + _this._reset(); + } + } + }, DragDropTouch._CTXMENU); + if (DragDropTouch._ISPRESSHOLDMODE) { + this._pressHoldInterval = setTimeout(function () { + _this._isDragEnabled = true; + _this._touchmove(e); + }, DragDropTouch._PRESSHOLDAWAIT); + } + } + } + } + }; + DragDropTouch.prototype._touchmove = function (e) { + if (this._shouldCancelPressHoldMove(e)) { + this._reset(); + return; + } + if (this._shouldHandleMove(e) || this._shouldHandlePressHoldMove(e)) { + // see if target wants to handle move + var target = this._getTarget(e); + if (this._dispatchEvent(e, 'mousemove', target)) { + this._lastTouch = e; + e.preventDefault(); + return; + } + // start dragging + if (this._dragSource && !this._img && this._shouldStartDragging(e)) { + this._dispatchEvent(e, 'dragstart', this._dragSource); + this._createImage(e); + this._dispatchEvent(e, 'dragenter', target); + } + // continue dragging + if (this._img) { + this._lastTouch = e; + e.preventDefault(); // prevent scrolling + if (target != this._lastTarget) { + this._dispatchEvent(this._lastTouch, 'dragleave', this._lastTarget); + this._dispatchEvent(e, 'dragenter', target); + this._lastTarget = target; + } + this._moveImage(e); + this._isDropZone = this._dispatchEvent(e, 'dragover', target); + } + } + }; + DragDropTouch.prototype._touchend = function (e) { + if (this._shouldHandle(e)) { + // see if target wants to handle up + if (this._dispatchEvent(this._lastTouch, 'mouseup', e.target)) { + e.preventDefault(); + return; + } + // user clicked the element but didn't drag, so clear the source and simulate a click + if (!this._img) { + this._dragSource = null; + this._dispatchEvent(this._lastTouch, 'click', e.target); + this._lastClick = Date.now(); + } + // finish dragging + this._destroyImage(); + if (this._dragSource) { + if (e.type.indexOf('cancel') < 0 && this._isDropZone) { + this._dispatchEvent(this._lastTouch, 'drop', this._lastTarget); + } + this._dispatchEvent(this._lastTouch, 'dragend', this._dragSource); + this._reset(); + } + } + }; + // ** utilities + // ignore events that have been handled or that involve more than one touch + DragDropTouch.prototype._shouldHandle = function (e) { + return e && + !e.defaultPrevented && + e.touches && e.touches.length < 2; + }; + + // use regular condition outside of press & hold mode + DragDropTouch.prototype._shouldHandleMove = function (e) { + return !DragDropTouch._ISPRESSHOLDMODE && this._shouldHandle(e); + }; + + // allow to handle moves that involve many touches for press & hold + DragDropTouch.prototype._shouldHandlePressHoldMove = function (e) { + return DragDropTouch._ISPRESSHOLDMODE && + this._isDragEnabled && e && e.touches && e.touches.length; + }; + + // reset data if user drags without pressing & holding + DragDropTouch.prototype._shouldCancelPressHoldMove = function (e) { + return DragDropTouch._ISPRESSHOLDMODE && !this._isDragEnabled && + this._getDelta(e) > DragDropTouch._PRESSHOLDMARGIN; + }; + + // start dragging when specified delta is detected + DragDropTouch.prototype._shouldStartDragging = function (e) { + var delta = this._getDelta(e); + return delta > DragDropTouch._THRESHOLD || + (DragDropTouch._ISPRESSHOLDMODE && delta >= DragDropTouch._PRESSHOLDTHRESHOLD); + } + + // clear all members + DragDropTouch.prototype._reset = function () { + this._destroyImage(); + this._dragSource = null; + this._lastTouch = null; + this._lastTarget = null; + this._ptDown = null; + this._isDragEnabled = false; + this._isDropZone = false; + this._dataTransfer = new DataTransfer(); + clearInterval(this._pressHoldInterval); + }; + // get point for a touch event + DragDropTouch.prototype._getPoint = function (e, page) { + if (e && e.touches) { + e = e.touches[0]; + } + return { x: page ? e.pageX : e.clientX, y: page ? e.pageY : e.clientY }; + }; + // get distance between the current touch event and the first one + DragDropTouch.prototype._getDelta = function (e) { + if (DragDropTouch._ISPRESSHOLDMODE && !this._ptDown) { return 0; } + var p = this._getPoint(e); + return Math.abs(p.x - this._ptDown.x) + Math.abs(p.y - this._ptDown.y); + }; + // get the element at a given touch event + DragDropTouch.prototype._getTarget = function (e) { + var pt = this._getPoint(e), el = document.elementFromPoint(pt.x, pt.y); + while (el && getComputedStyle(el).pointerEvents == 'none') { + el = el.parentElement; + } + return el; + }; + // create drag image from source element + DragDropTouch.prototype._createImage = function (e) { + // just in case... + if (this._img) { + this._destroyImage(); + } + // create drag image from custom element or drag source + var src = this._imgCustom || this._dragSource; + this._img = src.cloneNode(true); + this._copyStyle(src, this._img); + this._img.style.top = this._img.style.left = '-9999px'; + // if creating from drag source, apply offset and opacity + if (!this._imgCustom) { + var rc = src.getBoundingClientRect(), pt = this._getPoint(e); + this._imgOffset = { x: pt.x - rc.left, y: pt.y - rc.top }; + this._img.style.opacity = DragDropTouch._OPACITY.toString(); + } + // add image to document + this._moveImage(e); + document.body.appendChild(this._img); + }; + // dispose of drag image element + DragDropTouch.prototype._destroyImage = function () { + if (this._img && this._img.parentElement) { + this._img.parentElement.removeChild(this._img); + } + this._img = null; + this._imgCustom = null; + }; + // move the drag image element + DragDropTouch.prototype._moveImage = function (e) { + var _this = this; + requestAnimationFrame(function () { + if (_this._img) { + var pt = _this._getPoint(e, true), s = _this._img.style; + s.position = 'absolute'; + s.pointerEvents = 'none'; + s.zIndex = '999999'; + s.left = Math.round(pt.x - _this._imgOffset.x) + 'px'; + s.top = Math.round(pt.y - _this._imgOffset.y) + 'px'; + } + }); + }; + // copy properties from an object to another + DragDropTouch.prototype._copyProps = function (dst, src, props) { + for (var i = 0; i < props.length; i++) { + var p = props[i]; + dst[p] = src[p]; + } + }; + DragDropTouch.prototype._copyStyle = function (src, dst) { + // remove potentially troublesome attributes + DragDropTouch._rmvAtts.forEach(function (att) { + dst.removeAttribute(att); + }); + // copy canvas content + if (src instanceof HTMLCanvasElement) { + var cSrc = src, cDst = dst; + cDst.width = cSrc.width; + cDst.height = cSrc.height; + cDst.getContext('2d').drawImage(cSrc, 0, 0); + } + // copy style (without transitions) + var cs = getComputedStyle(src); + for (var i = 0; i < cs.length; i++) { + var key = cs[i]; + if (key.indexOf('transition') < 0) { + dst.style[key] = cs[key]; + } + } + dst.style.pointerEvents = 'none'; + // and repeat for all children + for (var i = 0; i < src.children.length; i++) { + this._copyStyle(src.children[i], dst.children[i]); + } + }; + DragDropTouch.prototype._dispatchEvent = function (e, type, target) { + if (e && target) { + var evt = document.createEvent('Event'), t = e.touches ? e.touches[0] : e; + evt.initEvent(type, true, true); + evt.button = 0; + evt.which = evt.buttons = 1; + this._copyProps(evt, e, DragDropTouch._kbdProps); + this._copyProps(evt, t, DragDropTouch._ptProps); + evt.dataTransfer = this._dataTransfer; + target.dispatchEvent(evt); + return evt.defaultPrevented; + } + return false; + }; + // gets an element's closest draggable ancestor + DragDropTouch.prototype._closestDraggable = function (e) { + for (; e; e = e.parentElement) { + if (e.hasAttribute('draggable') && e.draggable) { + return e; + } + } + return null; + }; + return DragDropTouch; + }()); + /*private*/ DragDropTouch._instance = new DragDropTouch(); // singleton + // constants + DragDropTouch._THRESHOLD = 5; // pixels to move before drag starts + DragDropTouch._OPACITY = 0.5; // drag image opacity + DragDropTouch._DBLCLICK = 500; // max ms between clicks in a double click + DragDropTouch._CTXMENU = 900; // ms to hold before raising 'contextmenu' event + DragDropTouch._ISPRESSHOLDMODE = false; // decides of press & hold mode presence + DragDropTouch._PRESSHOLDAWAIT = 400; // ms to wait before press & hold is detected + DragDropTouch._PRESSHOLDMARGIN = 25; // pixels that finger might shiver while pressing + DragDropTouch._PRESSHOLDTHRESHOLD = 0; // pixels to move before drag starts + // copy styles/attributes from drag source to drag image element + DragDropTouch._rmvAtts = 'id,class,style,draggable'.split(','); + // synthesize and dispatch an event + // returns true if the event has been handled (e.preventDefault == true) + DragDropTouch._kbdProps = 'altKey,ctrlKey,metaKey,shiftKey'.split(','); + DragDropTouch._ptProps = 'pageX,pageY,clientX,clientY,screenX,screenY,offsetX,offsetY'.split(','); + DragDropTouch_1.DragDropTouch = DragDropTouch; +})(DragDropTouch || (DragDropTouch = {})); diff --git a/index.html b/index.html index 0f1ba70..1bcf4ab 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@ +