Skip to content

Commit

Permalink
Fixed editor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed May 19, 2024
1 parent 8602990 commit 2481d21
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 55 deletions.
4 changes: 2 additions & 2 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 {ReactLogCaller} from '@/utils/logging';
import { ReactLogCaller } from '@/utils/logging';

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

const logger = ReactLogCaller
const logger = ReactLogCaller;

function App() {
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ 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
Expand Up @@ -111,10 +111,11 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve

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

const start = {
line: operation.path[0],
column: cursor.column + offset(cursor.line),
};
const start = { ...cursor, column: cursor.column + offset(cursor.line) };
// {
// line: operation.path[0],
// column: cursor.column + offset(cursor.line),
// };
const end = {
line: start.line,
column: start.column + operation.text.length,
Expand All @@ -140,7 +141,7 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
// Remove whole line
if (operation.path.length === 1) {
const start = { line: operation.path[0], column: 0 };
const end = { line: operation.path[0], column: Infinity };
const end = { ...start, column: Infinity };

const selection = { start, end };
return {
Expand All @@ -158,15 +159,9 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve

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

const start = {
...cursor,
column: cursor.column + lineOffset(cursor.line),
};
const start = { ...cursor, column: cursor.column + lineOffset(cursor.line) };

const end = {
...start,
column: start.column + operation.node.text.length,
};
const end = { ...start, column: start.column + operation.node.text.length };

const selection = { start, end };

Expand Down Expand Up @@ -219,10 +214,7 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve

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

const end = {
...start,
column: start.column + (offset || 0),
};
const end = { ...start, column: start.column + (offset || 0) };

return {
type: set_mode ? 'set_node' : 'unset_node',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default (editor: Editor, handlers: MarkdownDomainOperations) => {
const match = editor.above({
match: (n: Node) => Element.isElement(n) && editor.isBlock(n),
});
if(!match) return;
if (!match) return;
const [block, path] = match;
const start = Editor.start(editor, path);

Expand All @@ -169,24 +169,21 @@ export default (editor: Editor, handlers: MarkdownDomainOperations) => {
const deleteSelection = (deleteHandler: DeleteFunction, options?: TextDeleteOptions) => {
const selection = getSelection(editor);

console.log("Selection: ", selection);
console.log("Editor: ", editor.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);
}
}
if (!Element.isElement(block) || block.type === 'paragraph') continue;
// 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 and the block style
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
2 changes: 1 addition & 1 deletion code/client/src/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export const ClientLogCaller = {
export const DomainLogCaller = getLogger(ClientLogCaller.Domain);
export const ServicesLogCaller = getLogger(ClientLogCaller.Services);
export const ReactLogCaller = getLogger(ClientLogCaller.React);
export const PWALogCaller = getLogger(ClientLogCaller.PWA);
export const PWALogCaller = getLogger(ClientLogCaller.PWA);
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ function resourcesHandlers(service: ResourcesService, io: Server) {
* @param res
*/
const createResource = async (req: Request, res: Response) => {
// Validate workspace id
const { wid } = req.params;
if (!wid) throw new InvalidParameterError('Workspace id is required');
if (!isValidUUID(wid)) throw new InvalidParameterError('Invalid workspace id');

// Get resource input model
const resource = req.body as ResourceInputModel;
if (!resource) throw new InvalidParameterError('Body is required');
const { type, name, parent } = resource;
Expand All @@ -38,12 +41,17 @@ function resourcesHandlers(service: ResourcesService, io: Server) {
* @param res
*/
const getResource = async (req: Request, res: Response) => {
const { wid, id, metaOnly } = req.params;
// Validate workspace id and resource id
const { wid, id } = req.params;
if (!wid) throw new InvalidParameterError('Workspace id is required');
if (!id) throw new InvalidParameterError('Resource id is required');
if (!isValidUUID(wid)) throw new InvalidParameterError('Invalid workspace id');
if (!isValidUUID(id)) throw new InvalidParameterError('Invalid resource id');

// Get resource metadata query parameter
const { metaOnly } = req.query;
if (!isValidMetaOnlyValue(metaOnly as string)) throw new InvalidParameterError('Invalid metaOnly value');

const resource = await service.getResource(wid, id, metaOnly === 'true');
httpResponse.ok(res).json(resource);
};
Expand All @@ -54,14 +62,17 @@ function resourcesHandlers(service: ResourcesService, io: Server) {
* @param res
*/
const updateResource = async (req: Request, res: Response) => {
// Validate workspace id and resource id
const { wid, id } = req.params;
if (!wid) throw new InvalidParameterError('Workspace id is required');
if (!id) throw new InvalidParameterError('Resource id is required');
if (!isValidUUID(wid)) throw new InvalidParameterError('Invalid workspace id');
if (!isValidUUID(id)) throw new InvalidParameterError('Invalid resource id');

// Get resource input model
const resource = req.body as Partial<WorkspaceResource>;
if (!resource) throw new InvalidParameterError('Body is required');

await service.updateResource(id, resource);
io.in(wid).emit('updatedResource', { id, ...resource });
httpResponse.noContent(res).send();
Expand All @@ -73,6 +84,7 @@ function resourcesHandlers(service: ResourcesService, io: Server) {
* @param res
*/
const deleteResource = async (req: Request, res: Response) => {
// Validate workspace id and resource id
const { wid, id } = req.params;
if (!wid) throw new InvalidParameterError('Workspace id is required');
if (!id) throw new InvalidParameterError('Resource id is required');
Expand Down
4 changes: 2 additions & 2 deletions code/server/src/ts/controllers/ws/initSocketEvents.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SocketHandler } from '@controllers/ws/types';
import { Socket } from 'socket.io';

import {ControllersLogCaller} from '@src/utils/logging';
import { ControllersLogCaller } from '@src/utils/logging';

const logger = ControllersLogCaller("ws")
const logger = ControllersLogCaller('ws');

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

const logger = ServerLogCaller;
logger.logWarning('Starting server...');
Expand Down
13 changes: 6 additions & 7 deletions code/server/src/ts/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ export const ServiceLogCaller = {
};

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));

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));
10 changes: 3 additions & 7 deletions code/server/src/ts/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export function isValidUUID(uuid: string): boolean {
const regex = new RegExp('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-([89ab])[0-9a-fA-F]{3}-[0-9a-fA-F]{12}$');
return regex.test(uuid);
}
import { validate } from 'uuid';
export const isValidUUID = (uuid: string): boolean => validate(uuid);

export function isValidMetaOnlyValue(metaOnly?: string): boolean {
return !metaOnly || ['true', 'false'].includes(metaOnly);
}
export const isValidMetaOnlyValue = (metaOnly?: string): boolean => !metaOnly || ['true', 'false'].includes(metaOnly);

0 comments on commit 2481d21

Please sign in to comment.