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 31d56cf
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ export type HistoryHandlers = {
function historyHandlers(editor: Editor, domainOperations: HistoryDomainOperations): HistoryHandlers {
function undoOperation() {
const { history } = editor;
domainOperations.applyHistoryOperation(toHistoryOperations(editor, last(history.undos), true));
domainOperations.applyHistoryOperation(
toHistoryOperations(editor, last(history.undos), true)
);
}

function redoOperation() {
const { history } = editor;
domainOperations.applyHistoryOperation(toHistoryOperations(editor, last(history.redos), false));
domainOperations.applyHistoryOperation(
toHistoryOperations(editor, last(history.redos), false)
);
}

return { undoOperation, redoOperation };
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 All @@ -235,7 +227,9 @@ function toHistoryOperations(editor: Editor, operations: Batch | undefined, reve
return operations.operations
.map(operation => {
const type: BaseOperation['type'] = operation.type;
const operationType = reverseType ? getReverseType(type) : (type as HistoryOperation['type']);
const operationType = reverseType
? getReverseType(type)
: type as HistoryOperation['type'];
return toHistoryOperation(operationType, operations.selectionBefore, operation);
})
.filter(operation => operation !== undefined) as HistoryOperation[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,12 @@ export default (editor: Editor, handlers: MarkdownDomainOperations) => {
// 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);
}
Expand Down
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,18 @@ 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 +63,18 @@ 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 +86,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
1 change: 0 additions & 1 deletion code/server/src/ts/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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';

const logger = ServerLogCaller;
logger.logWarning('Starting server...');
Expand Down
11 changes: 4 additions & 7 deletions code/server/src/ts/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
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 31d56cf

Please sign in to comment.