diff options
author | Andrzej Hunt <andrzej.hunt@collabora.com> | 2015-10-30 19:45:09 +0100 |
---|---|---|
committer | Andrzej Hunt <andrzej.hunt@collabora.com> | 2015-10-30 19:47:51 +0100 |
commit | 23f1e4600a7914e5d2804c465cdb7b04b49d662e (patch) | |
tree | 3fae5ff7a160a077ff2f9dd3d92cd67bd2786c46 | |
parent | loleaflet: contextmenu plugin: don't override items list (diff) | |
download | online-feature/contextmenu.tar.gz online-feature/contextmenu.zip |
loleaflet: Implement context menu with 'copy' command
feature/contextmenu
We also have a dummy paste button, however this doesn't
actually work - it's possible that this might work in IE,
but is unsupported in most other browsers for security reasons.
-rw-r--r-- | loleaflet/build/deps.js | 1 | ||||
-rw-r--r-- | loleaflet/src/map/handler/Map.LOContextMenu.js | 82 |
2 files changed, 83 insertions, 0 deletions
diff --git a/loleaflet/build/deps.js b/loleaflet/build/deps.js index c9b3d1a99b..8c4a927fe6 100644 --- a/loleaflet/build/deps.js +++ b/loleaflet/build/deps.js @@ -222,6 +222,7 @@ var deps = { Mouse: { src: ['dom/DomEvent.MultiClick.js', + 'map/handler/Map.LOContextMenu.js', 'map/handler/Map.Mouse.js'], desc: 'Handles mouse interaction with the document.' }, diff --git a/loleaflet/src/map/handler/Map.LOContextMenu.js b/loleaflet/src/map/handler/Map.LOContextMenu.js new file mode 100644 index 0000000000..d3bfd3ecc6 --- /dev/null +++ b/loleaflet/src/map/handler/Map.LOContextMenu.js @@ -0,0 +1,82 @@ +/* + * L.Map.ContextMenu handles any document-related context menus + */ + +L.Map.mergeOptions({ + contextmenu: true, // enables contextmenu plugin + locontextmenu: true, // enables our internal contextmenu + contextmenuWidth: 140, + contextmenuItems: [{ + text: 'Copy', + callback: function (e) { + this.locontextmenu.menuCopy(e); + } + }, + '-', + { + text: 'Paste', + callback: function (e) { + this.locontextmenu.menuPaste(e); + + } + }] +}); + +L.Map.LOContextMenu = L.Handler.extend({ + + menuCopy: function(e) { + window.getSelection().removeAllRanges(); + var hidden = L.DomUtil.create('div', 'hidden-clipboard'); + hidden.textContent = decodeURIComponent(escape(this._map._docLayer._selectionTextContent)); + this._map._container.appendChild(hidden); + var range = document.createRange(); + range.selectNode(hidden); + window.getSelection().addRange(range); + + document.execCommand('copy'); + + window.getSelection().removeAllRanges(); + this._map._container.removeChild(hidden); + }, + + menuPaste: function(e) { + // THIS DOESN'T WORK + // execCommand('paste') isn't supported by most (all?) browsers + // We most likely can't actually access the clipboard. + var hidden = L.DomUtil.create('div', 'hidden-clipboard'); + this._map._container.appendChild(hidden); + var range = document.createRange(); + range.selectNode(hidden); + window.getSelection().addRange(range); + hidden.focus(); + + document.execCommand('paste'); + + if (hidden.textContent && hidden.textContent.length > 0) { + L.Socket.sendMessage('paste mimetype=text/plain;charset=utf-8 data=' + hidden.textContent); + } + + window.getSelection().removeAllRanges(); + this._map._container.removeChild(hidden); + }, + + initialize: function (map) { + this._map = map; + }, + + addHooks: function () { + this._map.on('contextmenu', + this._onContextMenu, this); + }, + + removeHooks: function () { + this._map.off('contextmenu', + this._onContextMenu, this); + }, + + _onContextMenu: function (e) { + this._map.contextmenu.showAt(e.latlng, {}); + }, +}); + +L.Map.addInitHook('addHandler', 'locontextmenu', L.Map.LOContextMenu); |