Skip to content

Commit

Permalink
Editor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 25, 2024
1 parent 1bc8a2c commit acae4e5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default (fugue: Fugue, serviceConnector: ServiceConnector): MarkdownConne
const cursor = { line, column: 0 };
const nodes = Array.from(fugue.traverseBySeparator(' ', cursor, false, true));
const idsToDelete: Id[] = nodes[0].map(node => node.id);
const deleteOperations = fugue.deleteLocalById(cursor, ...idsToDelete);
const deleteOperations = fugue.deleteLocalById(...idsToDelete);
operations.push(...deleteOperations);
}

Expand Down
21 changes: 9 additions & 12 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,16 @@ export class Fugue {
parent,
side: isEmpty(leftOrigin.rightChildren) ? 'R' : 'L',
styles,
line: cursor.line,
};
}

/**
* Inserts a new node in the tree based on the given operation.
* @param line
* @param id
* @param value
* @param parent
* @param side
* @param styles
* @param operation
*/
private addNode = ({ id, value, parent, side, styles }: InsertOperation) => {
this.tree.addNode(id, value, parent, side, styles || []);
private addNode = ({id, value, line, styles, parent, side}: InsertOperation) => {
this.tree.addNode(id, value, parent, side, styles, line);
};

/**
Expand Down Expand Up @@ -297,16 +293,17 @@ export class Fugue {
* @param cursor
*/
getNodeByCursor({ line, column }: Cursor): FugueNode | undefined {
if (line === 0 && column === 0) return this.tree.root;
if (column === 0) {
return this.traverseBySeparator('\n', { line: line - 1, column: 1 }, false, true).next().value?.[0];
}
if (column === 0) return this.getLineRoot(line);
const start = { line, column: column - 1 };
const end = { line, column };
const iterator = this.traverseBySelection({ start, end });
return iterator.next().value;
}

getLineRoot = (line: number): FugueNode => {
return line === 0 ? this.tree.root : this.tree.root.value[line - 1];
}

/**
* Returns the string representation of the tree.
*/
Expand Down
10 changes: 7 additions & 3 deletions code/client/src/domain/editor/fugue/FugueTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ export class FugueTree<T> {
* @param parent the id of the parent node
* @param side the side of the parent node where this node is located
* @param styles the styles of the node
* @param line
*/
addNode(id: Id, value: T, parent: Id | null, side: 'L' | 'R', styles?: InlineStyle[] | BlockStyle[]) {
addNode(id: Id, value: T, parent: Id, side: 'L' | 'R', styles: InlineStyle[] | undefined, line: number){
// create node
const node = treeNode(id, value, parent, side, 0, styles as InlineStyle[]);
const node : Node<T> = treeNode(id, value, parent, side, 0, styles as InlineStyle[]);
if(value === '\n') {
this._root.value.splice(line, 0, node); // TODO: check if this is correct
}
this._addNode(node);
}

Expand Down Expand Up @@ -81,7 +85,7 @@ export class FugueTree<T> {
if (!node.isDeleted) node.isDeleted = true;
if (node.value === '\n') {
const idx = this._root.value.findIndex(n => n.id === id);
this._root.value[idx].isDeleted = true;
this._root.value.splice(idx, 1); // TODO: check if this is correct
}
}

Expand Down
1 change: 1 addition & 0 deletions code/shared/src/document/types/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type InsertOperation = {
parent: Id;
side: "L" | "R";
styles?: InlineStyle[];
line: number;
};

export type DeleteOperation = {
Expand Down

0 comments on commit acae4e5

Please sign in to comment.