Skip to content

Commit

Permalink
Fixed Editor Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 23, 2024
1 parent a01b696 commit 73526d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export default (fugue: Fugue, serviceConnector: ServiceConnector): MarkdownConne
}

function deleteBlockStyles(selection: Selection) {
if (isSelectionEmpty(selection)) return;
const { start, end } = selection;
const inStartOfLine = isEqual(start, end) && start.column === 0;
const isMultiLine = start.line !== end.line;
Expand Down
43 changes: 20 additions & 23 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ export class Fugue {
* @param selection
*/
reviveLocal(selection: Selection): ReviveOperation[] {
const _selection = { start: { ...selection.start }, end: { ...selection.end } };
const nodes = Array.from(this.traverseBySelection(_selection, true));
const nodes = Array.from(this.traverseBySelection(selection, true));
return nodes.map(node => {
if (node.value === '\n') {
_selection.start.line++;
_selection.start.column = 0;
} else _selection.start.column++;
selection.start.line++;
selection.start.column = 0;
} else selection.start.column++;

return this.reviveNode(node.id, _selection.start);
return this.reviveNode(node.id, selection.start);
});
}

Expand All @@ -186,7 +185,7 @@ export class Fugue {
* @param cursor
*/
reviveLocalByCursor(cursor: Cursor) {
const node = this.getNodeByCursor(cursor, true);
const node = this.getNodeByCursor(cursor);
if (node) return this.reviveNode(node.id, cursor);
}

Expand Down Expand Up @@ -270,7 +269,7 @@ export class Fugue {

/**
* Traverses the tree by the given selection
* @param selection - the selection from which to traverse, in format [start, end[
* @param selection - the selection from which to traverse, in format ]start, end]
* @param returnDeleted
*/
*traverseBySelection(selection: Selection, returnDeleted: boolean = false): IterableIterator<FugueNode> {
Expand All @@ -281,20 +280,20 @@ export class Fugue {
// const lineRootNode = this.tree.getLineRoot(start.line);

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

// yield node if in bounds
if (inBounds) yield node;

// update counters
if (node.value === '\n') {
lineCounter++;
columnCounter = 0;
} else {
columnCounter++;
}
// check if in bounds
if (lineCounter === start.line && columnCounter === start.column) inBounds = true;

// yield node if in bounds
if (inBounds) yield node;

// end condition - check if next node is out of bounds
// end condition
if (lineCounter === end.line && columnCounter === end.column) break;
}
}
Expand All @@ -314,7 +313,7 @@ export class Fugue {
inclusive: boolean = false
): IterableIterator<FugueNode[]> {
const selection = reverse
? { start: { line, column: 1 }, end: { line, column } }
? { start: { line, column: 0 }, end: { line, column } }
: { start: { line, column }, end: { line, column: Infinity } };

const nodesInSelection = Array.from(this.traverseBySelection(selection));
Expand Down Expand Up @@ -346,14 +345,12 @@ export class Fugue {
/**
* Returns the node at the given cursor
* @param cursor
* @param returnDeleted
*/
getNodeByCursor({ line, column }: Cursor, returnDeleted: boolean = false): FugueNode | undefined {
//if (column === 0) return this.tree.getLineRoot(line);
if (line === 0 && column === 0) return this.tree.root;
const start = { line, column };
const end = { line, column: column };
const iterator = this.traverseBySelection({ start, end }, returnDeleted);
getNodeByCursor({ line, column }: Cursor): FugueNode | undefined {
if (column === 0) return this.tree.getLineRoot(line);
const start = { line, column: column - 1 };
const end = { line, column };
const iterator = this.traverseBySelection({ start, end });
return iterator.next().value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getSelection } from '@domain/editor/slate/utils/selection';
import { TextDeleteOptions } from 'slate/dist/interfaces/transforms/text';
import { RuleType } from '@domain/editor/slate/plugins/markdown/rules';
import { BlockStyle } from '@notespace/shared/src/document/types/styles';
import { MarkdownConnector } from '@domain/editor/connectors/markdown/connector';
import { MarkdownConnector } from '@domain/editor/connectors/markdown/types';

type InlineFunction = (n: Element) => boolean;
type DeleteBackwardFunction = (unit: TextUnit, options?: { at: Range }) => void;
Expand Down

0 comments on commit 73526d2

Please sign in to comment.