From 77d3a6c41743a92f264a67ee23cb83bba1c77927 Mon Sep 17 00:00:00 2001 From: HF Date: Tue, 28 Apr 2020 07:16:47 +0200 Subject: [PATCH] click on coords box copies to chat if open style changes, chat now selectable --- src/components/Chat.jsx | 9 ++++++-- src/components/CoordinatesBox.jsx | 24 +++++++++++++++++--- src/components/HelpModal.jsx | 5 +++-- src/components/NotifyBox.jsx | 13 ++++++----- src/components/base.tcss | 4 ++++ src/controls/keypress.js | 12 +++++++++- src/utils/clipboard.js | 37 +++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 src/utils/clipboard.js diff --git a/src/components/Chat.jsx b/src/components/Chat.jsx index 9974b85..4feb0f2 100644 --- a/src/components/Chat.jsx +++ b/src/components/Chat.jsx @@ -40,15 +40,20 @@ const Chat = ({ chatMessages }) => {
+   - {` ${message[0]}: `} + {`${message[0]}`} + :   { splitCoordsInString(message[1]).map((text, i) => { if (i % 2 === 0) { diff --git a/src/components/CoordinatesBox.jsx b/src/components/CoordinatesBox.jsx index 0e8fbb8..3151572 100644 --- a/src/components/CoordinatesBox.jsx +++ b/src/components/CoordinatesBox.jsx @@ -5,6 +5,8 @@ import React from 'react'; import { connect } from 'react-redux'; +import copy from '../utils/clipboard'; +import { notify } from '../actions'; import type { State } from '../reducers'; @@ -13,17 +15,33 @@ function renderCoordinates(cell): string { return `(${cell.join(', ')})`; } -const CoordinatesBox = ({ view, hover }) => ( -
{ + +const CoordinatesBox = ({ view, hover, notifyCopy }) => ( +
{ copy(location.hash); notifyCopy(); }} + role="button" + title="Copy to Clipboard" + tabIndex="0" + >{ renderCoordinates(hover || view.map(Math.round)) }
); +function mapDispatchToProps(dispatch) { + return { + notifyCopy() { + dispatch(notify('Copied!')); + }, + }; +} + function mapStateToProps(state: State) { const { view } = state.canvas; const { hover } = state.gui; return { view, hover }; } -export default connect(mapStateToProps)(CoordinatesBox); +export default connect(mapStateToProps, mapDispatchToProps)(CoordinatesBox); diff --git a/src/components/HelpModal.jsx b/src/components/HelpModal.jsx index 8f409f6..79bdc4a 100644 --- a/src/components/HelpModal.jsx +++ b/src/components/HelpModal.jsx @@ -64,8 +64,9 @@ const HelpModal = () => (

2D Controls

Click a color in palette to select

Press G to toggle grid

-

Press C to toggle showing of pixel activity

-

Press q or e to zoom

+

Press X to toggle showing of pixel activity

+

Press C to copy coordinates

+

Press Q or E to zoom

Press W,A,S, D to move

Press ,,, to move

Drag mouse to move

diff --git a/src/components/NotifyBox.jsx b/src/components/NotifyBox.jsx index d5e8612..0006f07 100644 --- a/src/components/NotifyBox.jsx +++ b/src/components/NotifyBox.jsx @@ -8,14 +8,17 @@ import { connect } from 'react-redux'; import type { State } from '../reducers'; -let style = {}; function getStyle(notification) { - if (notification) { - style = { - backgroundColor: (notification >= 0) ? '#a9ffb0cc' : '#ffa9a9cc', + if (!notification) return {}; + + if (typeof notification === 'string') { + return { + width: 50, }; } - return style; + return { + backgroundColor: (notification >= 0) ? '#a9ffb0cc' : '#ffa9a9cc', + }; } const NotifyBox = ({ notification }) => ( diff --git a/src/components/base.tcss b/src/components/base.tcss index 3bc8c0e..5193967 100644 --- a/src/components/base.tcss +++ b/src/components/base.tcss @@ -116,6 +116,7 @@ kbd { } :global(.coorbox) { left: 16px; + cursor: pointer; bottom: 16px; } :global(.onlinebox) { @@ -262,10 +263,12 @@ kbd { :global(.chatname) { color: #4B0000; font-size: 13px; + user-select: all; } :global(.chatmsg) { color: #030303; font-size: 13px; + user-select: text; margin: 0; } @@ -288,6 +291,7 @@ kbd { } :global(.notifyboxvis), :global(.notifyboxhid) { + background-color: rgba(226, 226, 226, 0.80); position: absolute; top: 57px; left: 0px; diff --git a/src/controls/keypress.js b/src/controls/keypress.js index 597ca50..37ab4c3 100644 --- a/src/controls/keypress.js +++ b/src/controls/keypress.js @@ -5,10 +5,12 @@ import keycode from 'keycode'; import store from '../ui/store'; +import copy from '../utils/clipboard'; import { toggleGrid, togglePixelNotify, toggleMute, + notify, } from '../actions'; @@ -24,12 +26,20 @@ function onKeyPress(event: KeyboardEvent) { case 'g': store.dispatch(toggleGrid()); break; - case 'c': + case 'x': store.dispatch(togglePixelNotify()); break; case 'm': store.dispatch(toggleMute()); break; + case 'c': { + const state = store.getState(); + const { hover } = state.gui; + const text = hover.join('_'); + copy(text); + store.dispatch(notify('Copied!')); + break; + } default: } } diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js new file mode 100644 index 0000000..ef7ebef --- /dev/null +++ b/src/utils/clipboard.js @@ -0,0 +1,37 @@ +/* @flow + */ + +function fallbackCopyTextToClipboard(text) { + const textArea = document.createElement('textarea'); + textArea.value = text; + + // Avoid scrolling to bottom + textArea.style.top = '0'; + textArea.style.left = '0'; + textArea.style.position = 'fixed'; + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + const successful = document.execCommand('copy'); + document.body.removeChild(textArea); + return successful; + } catch { + document.body.removeChild(textArea); + return false; + } +} + +export default async function copyTextToClipboard(text) { + if (!navigator.clipboard) { + return fallbackCopyTextToClipboard(text); + } + try { + await navigator.clipboard.writeText(text); + return true; + } catch { + return false; + } +}