Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cursor display #12

Merged
merged 3 commits into from Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -7,6 +7,7 @@
"actual": "^0.4.0",
"babel-polyfill": "^6.26.0",
"codemirror": "^5.33.0",
"color-convert": "^1.9.1",
"diff": "^3.4.0",
"finalhandler": "^1.1.0",
"github-markdown-css": "^2.10.0",
Expand Down
35 changes: 22 additions & 13 deletions src/AppEnabledWikiEditorCodeMirror.jsx
Expand Up @@ -5,6 +5,7 @@ import { UnControlled as CodeMirror } from 'react-codemirror2';
import 'codemirror/mode/markdown/markdown';
import SplitPane from 'react-split-pane';
import io from 'socket.io-client';
import ColorConvert from 'color-convert';

import Y from 'yjs/dist/y.es6';
import yArray from 'y-array/dist/y-array.es6';
Expand All @@ -23,22 +24,30 @@ const resizerMargin = 12;
class OtherClientCursor {
constructor(id) {
this.id = id;
this.setColor(id);
}
setColor(hashId) {
this.color = `#${hashId.slice(0, 6)}`;
let hue = 0;
for (let i = 0; i < id.length; i += 1) {
hue *= 2;
hue += id.charCodeAt(i);
hue %= 360;
}
this.color = `#${ColorConvert.hsv.hex(hue, 100, 100)}`;
}
updateCursor(cursorPos, cm) {
this.removeCursor();
const cursorCoords = cm.cursorCoords(cursorPos);
const cursorElement = document.createElement('span');
cursorElement.style.borderLeftStyle = 'solid';
cursorElement.style.borderLeftWidth = '2px';
cursorElement.style.borderLeftColor = this.color;
cursorElement.style.height = `${(cursorCoords.bottom - cursorCoords.top)}px`;
cursorElement.style.padding = 0;
cursorElement.style.zIndex = 0;
this.marker = cm.setBookmark(cursorPos, { widget: cursorElement, insertLeft: true });
const svgSize = 8;
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', svgSize);
svg.setAttribute('height', svgSize);
svg.setAttribute('viewBox', `0 0 ${svgSize} ${svgSize}`);
svg.style.position = 'absolute';
svg.style.marginLeft = `-${svgSize / 2}px`;
svg.style.marginTop = `${cm.defaultTextHeight()}px`;
const polyline = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
polyline.setAttribute('points', `0 ${svgSize}, ${svgSize / 2} 0, ${svgSize} ${svgSize}, 0 ${svgSize}`);
polyline.setAttribute('fill', this.color);
polyline.setAttribute('fill-opacity', 0.9);
svg.appendChild(polyline);
this.marker = cm.setBookmark(cursorPos, { widget: svg, insertLeft: true });
}
removeCursor() {
if (this.marker) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -2344,7 +2344,7 @@ collapse-white-space@^1.0.0, collapse-white-space@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/collapse-white-space/-/collapse-white-space-1.0.3.tgz#4b906f670e5a963a87b76b0e1689643341b6023c"

color-convert@^1.3.0, color-convert@^1.9.0:
color-convert@^1.3.0, color-convert@^1.9.0, color-convert@^1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed"
dependencies:
Expand Down