Skip to content

Commit

Permalink
Fixed editor bug when deleting multiple lines
Browse files Browse the repository at this point in the history
* Before - last line block style wasn't deleted
* All styles get deleted
  • Loading branch information
GuilhermeF03 committed May 19, 2024
1 parent d2b4a4d commit 8602990
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 28 deletions.
6 changes: 3 additions & 3 deletions code/client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { ErrorProvider } from '@ui/contexts/error/ErrorContext';
import Sidebar from '@ui/components/sidebar/Sidebar';
import { WorkspaceProvider } from '@ui/contexts/workspace/WorkspaceContext';
import Home from '@ui/pages/home/Home';
import { ClientLogCaller } from '@/utils/logging';
import getLogger from '@notespace/shared/src/utils/logging';
import {ReactLogCaller} from '@/utils/logging';

import { CommunicationProvider } from '@ui/contexts/communication/CommunicationContext';
import { useEffect } from 'react';

const logger = getLogger(ClientLogCaller.React);
const logger = ReactLogCaller

function App() {
useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions code/client/src/domain/editor/operations/input/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ 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;
// 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 @@ -73,7 +73,9 @@ export default (fugue: Fugue, { socket }: Communication): MarkdownDomainOperatio

// Remove block styles if the selection is single position at beginning of a line or multi-line selection
if ((start === end && start.column === 0) || start.line !== end.line) {
const newSelection = start.column !== 0 ? { start: { line: start.line + 1, column: 0 }, end } : selection;
const newSelection = start.column !== 0
? { start: { line: start.line + 1, column: 0 }, end }
: selection;
const operations = fugue.updateBlockStylesLocalBySelection('paragraph', newSelection);
socket.emit('operations', operations);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, Element, Node, Point, Range, Text, type TextUnit, Transforms } from 'slate';
import { Editor, Element, Node, Point, Range, Text, Location, type TextUnit, Transforms } from 'slate';
import { shortcuts } from '../shortcuts';
import CustomEditor from '@domain/editor/slate/CustomEditor';
import { isMultiBlock } from '@domain/editor/slate/utils/slate';
Expand Down Expand Up @@ -152,29 +152,41 @@ export default (editor: Editor, handlers: MarkdownDomainOperations) => {
const match = editor.above({
match: (n: Node) => Element.isElement(n) && editor.isBlock(n),
});
if (match) {
const [block, path] = match;
const start = Editor.start(editor, path);
if (
!Editor.isEditor(block) &&
Element.isElement(block) &&
block.type !== 'paragraph' &&
Point.equals(selection.anchor, start)
) {
const { line } = getSelection(editor).start;
Transforms.setNodes(editor, { type: 'paragraph' });
handlers.applyBlockStyle('paragraph', line);
return;
}
if(!match) return;
const [block, path] = match;
const start = Editor.start(editor, path);

// If the block is not a paragraph and the selection is at the start of the block, delete the block style
if (Element.isElement(block) && block.type !== 'paragraph' && Point.equals(selection.anchor, start)) {
const newSelection = getSelection(editor);
Transforms.setNodes(editor, { type: 'paragraph' });
handlers.deleteBlockStyles(newSelection);
return;
}
deleteBackward(...args);
};

const deleteSelection = (deleteHandler: DeleteFunction, options?: TextDeleteOptions) => {
const selection = getSelection(editor);
handlers.deleteBlockStyles(selection);

console.log("Selection: ", selection);
console.log("Editor: ", editor.selection);

// Iterate over the selected lines and delete the block styles
for (let i = selection.start.line + 1; i <= selection.end.line; i++) {
const block = editor.children[i];
if (Element.isElement(block)) {
// If the block is not a paragraph and the selection is at the start of the block, delete the block style
// Else remove both the block
if(block.type !== 'paragraph'){
const location : Location = {path: [i, 0], offset: 0};
Transforms.setNodes(editor, { type: 'paragraph' }, { at: location });
handlers.deleteBlockStyles(selection);
}
}
}
deleteHandler(options);
};
}

/**
* Checks if the given node is an inline.
Expand Down
6 changes: 6 additions & 0 deletions code/client/src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { ColorWrap, LogColor } from '@notespace/shared/src/utils/logging';
import getLogger from '@notespace/shared/src/utils/logging';

export const ClientLogCaller = {
React: ColorWrap(LogColor.Blue, 'React'),
Services: ColorWrap(LogColor.Yellow, 'Services'),
Domain: ColorWrap(LogColor.Green, 'Domain'),
PWA: ColorWrap(LogColor.Red, 'PWA'),
};

export const DomainLogCaller = getLogger(ClientLogCaller.Domain);
export const ServicesLogCaller = getLogger(ClientLogCaller.Services);
export const ReactLogCaller = getLogger(ClientLogCaller.React);
export const PWALogCaller = getLogger(ClientLogCaller.PWA);
5 changes: 2 additions & 3 deletions code/server/src/ts/controllers/ws/initSocketEvents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { SocketHandler } from '@controllers/ws/types';
import { Socket } from 'socket.io';

import { ServiceLogCaller } from '@src/utils/logging';
import getLogger, { ColorWrap, LogColor } from '@notespace/shared/src/utils/logging';
import {ControllersLogCaller} from '@src/utils/logging';

const logger = getLogger(ServiceLogCaller.Controllers + '-' + ColorWrap(LogColor.Red, 'ws'));
const logger = ControllersLogCaller("ws")

export default function initSocketEvents(events: Record<string, SocketHandler>) {
// const onCursorChange = events['cursorChange'];
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import router from '@src/controllers/http/router';
import config from '@src/config';
import initSocketEvents from '@controllers/ws/initSocketEvents';
import { TestDatabases } from '@databases/TestDatabases';
import { ServiceLogCaller } from '@src/utils/logging';
import {ServerLogCaller} from '@src/utils/logging';
import getLogger from '@notespace/shared/src/utils/logging';

const logger = getLogger(ServiceLogCaller.Server);
const logger = ServerLogCaller;
logger.logWarning('Starting server...');

// setup services and databases
Expand Down
10 changes: 10 additions & 0 deletions code/server/src/ts/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { ColorWrap, LogColor } from '@notespace/shared/src/utils/logging';
import getLogger from '@notespace/shared/src/utils/logging';

export const ServiceLogCaller = {
Server: ColorWrap(LogColor.Blue, 'Server'),
Database: ColorWrap(LogColor.Green, 'Database'),
Services: ColorWrap(LogColor.Yellow, 'Services'),
Controllers: ColorWrap(LogColor.Red, 'Controllers'),
};

export const ServerLogCaller = getLogger(ServiceLogCaller.Server);
export const DatabaseLogCaller = (module : string) =>
getLogger(ServiceLogCaller.Database + '-' + ColorWrap(LogColor.Green, module));
export const ServicesLogCaller = (module : string) =>
getLogger(ServiceLogCaller.Services + '-' + ColorWrap(LogColor.Yellow, module));
export const ControllersLogCaller = (module : string) =>
getLogger(ServiceLogCaller.Controllers + '-' + ColorWrap(LogColor.Red, module));

0 comments on commit 8602990

Please sign in to comment.