Skip to content

Commit

Permalink
Updates on history
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed May 19, 2024
1 parent 494dcd3 commit c4b0d11
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion code/client/src/domain/editor/crdt/fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export class Fugue {
inBounds = false;

const lineRootNode = this.tree.getLineRoot(start.line);
for (const node of this.tree.traverse(lineRootNode, returnDeleted)) {

const root = lineRootNode || this.tree.root; // used when reversing a selection that starts at the beginning of a line

for (const node of this.tree.traverse(root, returnDeleted)) {
// start condition
if (lineCounter === start.line && columnCounter === start.column) inBounds = true;

Expand Down
6 changes: 6 additions & 0 deletions code/client/src/domain/editor/operations/input/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { nodeInsert } from '@domain/editor/crdt/utils';
import { InlineStyle } from '@notespace/shared/src/document/types/styles';
import { Operation } from '@notespace/shared/src/document/types/operations';
import { Communication } from '@services/communication/communication';
import {isEqual} from "lodash";

export default (fugue: Fugue, { socket }: Communication): InputDomainOperations => {
function insertCharacter(char: string, cursor: Cursor, styles: InlineStyle[] = []) {
Expand All @@ -28,6 +29,11 @@ export default (fugue: Fugue, { socket }: Communication): InputDomainOperations
}

function deleteSelection(selection: Selection) {
if(isEqual(selection.start, selection.end)) return;

if(selection.start.column === 0) selection.start.column += 1;
if(selection.end.column === 0) selection.end.column += 1;

const operations = fugue.deleteLocal(selection);
socket.emit('operations', operations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
case 'insert_text':
return insertTextOperation(operation as BaseInsertTextOperation);
case 'remove_text':
return removeTextOperation(operation as BaseRemoveTextOperation);
return removeTextOperation(operation as BaseRemoveTextOperation, selectionBefore);
case 'insert_node':
return nodeOperation(operation as BaseInsertNodeOperation, selectionBefore, true);
case 'remove_node':
Expand Down Expand Up @@ -98,13 +98,16 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
/**
* Converts a slate remove text operation to a history remove text operation
* @param operation
* @param selectionBefore
*/
function removeTextOperation(operation: BaseRemoveTextOperation): RemoveTextOperation | undefined {
function removeTextOperation(operation: BaseRemoveTextOperation, selectionBefore : BaseRange | null): RemoveTextOperation | undefined {
const offset = (line: number) => (line === 0 ? 0 : 1);

if (operation.text === '') return undefined;
if(!selectionBefore) return undefined;

const cursor = pointToCursor(editor, {...selectionBefore?.anchor});

const cursor = pointToCursor(editor, { path: operation.path, offset: operation.offset });

const start = {
line: operation.path[0],
Expand Down Expand Up @@ -134,8 +137,8 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve

// Remove whole line
if (operation.path.length === 1) {
const start = pointToCursor(editor, { path: operation.path, offset: 0 });
const end = pointToCursor(editor, { path: [operation.path[0] + 1, 0], offset: 0 });
const start = { line: operation.path[0], column: 0}
const end = { line: operation.path[0], column: Infinity}

const selection = { start, end };
return {
Expand Down
5 changes: 4 additions & 1 deletion code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ const pointsToSelection = (editor: Editor, start: Point, end: Point): Selection
*/
export function pointToCursor(editor: Editor, point: Point): Cursor {
const line = point.path[0];
const children = Array.from(Node.children(editor, [line]));
const cursor: Cursor = { line, column: point.offset };

if (point.path[1] === 0) return cursor;

const children = Array.from(Node.children(editor, [line]));

for (const entry of children) {
// If path has only one element, and it is the same as the first element of the point path - same line
if (point.path.length === 1 && point.path[0] === entry[1][0]) break;
Expand Down
2 changes: 1 addition & 1 deletion code/server/src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ const socketEvents = initSocketEvents(events);
io.on('connection', socketEvents);

server.listen(config.SERVER_PORT, config.SERVER_IP, () => {
logger.logSuccess(`listening on http://${config.SERVER_IP}:${config.SERVER_PORT}`);
logger.logSuccess(`Listening on http://${config.SERVER_IP}:${config.SERVER_PORT}`);
});

0 comments on commit c4b0d11

Please sign in to comment.