-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ed1f39
commit f7c62d2
Showing
25 changed files
with
143 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Selection } from '@notespace/shared/types/cursor'; | ||
|
||
type CursorProps = { | ||
selection: Selection; | ||
color: string; | ||
}; | ||
|
||
function Cursor({ selection, color }: CursorProps) { | ||
// TODO: Get absolute position of cursor in pixels using selection and render cursor/selection accordingly | ||
console.log(selection); | ||
return ( | ||
<div | ||
className="cursor" | ||
style={{ | ||
position: 'absolute', | ||
top: `px`, | ||
left: `px`, | ||
width: '2px', | ||
height: '1.5em', | ||
backgroundColor: color, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export default Cursor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Selection } from '../../../../../shared/types/cursor.ts'; | ||
|
||
export type CursorData = { | ||
id: string; | ||
selection: Selection; | ||
color: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,24 @@ | ||
import { useState } from 'react'; | ||
import './CursorsManager.scss'; | ||
import useSocketListeners from '../../../socket/useSocketListeners.ts'; | ||
|
||
interface Cursor { | ||
id: string; | ||
position: { line: number; column: number }; | ||
color: string; | ||
} | ||
import { CursorData } from '@editor/components/cursors/CursorData.ts'; | ||
import Cursor from '@editor/components/cursors/Cursor.tsx'; | ||
|
||
function Cursors() { | ||
const [cursors, setCursors] = useState<Cursor[]>([]); | ||
const [cursors, setCursors] = useState<CursorData[]>([]); | ||
|
||
const handleCursorChange = (cursor: Cursor) => { | ||
const onCursorChange = (cursor: CursorData) => { | ||
setCursors(prevCursors => { | ||
const updatedCursors = prevCursors.filter(c => c.id !== cursor.id); | ||
return [...updatedCursors, cursor]; | ||
}); | ||
}; | ||
|
||
useSocketListeners({ | ||
cursorChange: handleCursorChange, | ||
cursorChange: onCursorChange, | ||
}); | ||
|
||
const getCharacterWidth = (textarea: HTMLElement) => { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d')!; | ||
const computedStyle = getComputedStyle(textarea); | ||
ctx.font = computedStyle.font; | ||
return ctx.measureText('W').width; | ||
}; | ||
|
||
const renderCursors = () => { | ||
return cursors.map(cursor => { | ||
const textarea = document.querySelector('textarea')!; | ||
const fontSize = parseFloat(getComputedStyle(textarea).fontSize); | ||
const lineHeight = fontSize * 1.2; | ||
const characterWidth = getCharacterWidth(textarea); | ||
const top = textarea.offsetTop + (cursor.position.line - 1) * lineHeight - 2; | ||
const left = textarea.offsetLeft + (cursor.position.column - 1) * characterWidth + 1; | ||
return ( | ||
<div | ||
className="cursor" | ||
key={cursor.id} | ||
style={{ | ||
position: 'absolute', | ||
top: `${top}px`, | ||
left: `${left}px`, | ||
width: '2px', | ||
height: '1.5em', | ||
backgroundColor: cursor.color, | ||
}} | ||
/> | ||
); | ||
}); | ||
}; | ||
return <>{renderCursors()}</>; | ||
return cursors.map(cursor => <Cursor key={cursor.id} color={cursor.color} selection={cursor.selection} />); | ||
} | ||
|
||
export default Cursors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
code/client/src/editor/slate/plugins/markdown/rendering/elements.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { io } from 'socket.io-client'; | ||
|
||
const SOCKET_SERVER_URL = 'ws://localhost:8080'; | ||
const socketConfig = {}; | ||
export const socket = io(SOCKET_SERVER_URL, socketConfig); | ||
export const socket = io(SOCKET_SERVER_URL); |
Oops, something went wrong.