Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPIKE] Add delete to start of line for MacOS #636

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/js/editor/event-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export default class EventManager {
let unit = 'char';
if (key.altKey && Browser.isMac()) {
unit = 'word';
} else if (key.metaKey && Browser.isMac()) {
unit = 'line';
} else if (key.ctrlKey && Browser.isWin()) {
unit = 'word';
}
Expand Down
11 changes: 10 additions & 1 deletion src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,16 @@ class PostEditor {
this.toggleSection('p', position);
return this._range.head;
} else {
let prevPosition = unit === 'word' ? position.moveWord(BACKWARD) : position.move(BACKWARD);
let prevPosition;

if (unit === 'word') {
prevPosition = position.moveWord(BACKWARD);
} else if (unit === 'line') {
prevPosition = Position.atStartOfLine(position, this.editor);
} else {
prevPosition = position.move(BACKWARD);
}

let range = prevPosition.toRange(position);
return this.deleteRange(range);
}
Expand Down
52 changes: 52 additions & 0 deletions src/js/utils/cursor/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ function findOffsetInSection(section, node, offset) {
}
}

// TODO: expose as utility function, update cursor._findNodeForPosition
function findNodeForPosition(position) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have been copied from cursor.js, the code should probably be de-duplicated?

let { section } = position;
let node, offset;
if (section.isCardSection) {
offset = 0;
if (position.offset === 0) {
node = section.renderNode.element.firstChild;
} else {
node = section.renderNode.element.lastChild;
}
} else if (section.isBlank) {
node = section.renderNode.cursorElement;
offset = 0;
} else {
let { marker, offsetInMarker } = position;
if (marker.isAtom) {
if (offsetInMarker > 0) {
// FIXME -- if there is a next marker, focus on it?
offset = 0;
node = marker.renderNode.tailTextNode;
} else {
offset = 0;
node = marker.renderNode.headTextNode;
}
} else {
node = marker.renderNode.element;
offset = offsetInMarker;
}
}

return { node, offset };
}

let Position, BlankPosition;

Position = class Position {
Expand Down Expand Up @@ -101,6 +135,24 @@ Position = class Position {
return Position.fromNode(_renderTree, node, offset);
}

static atStartOfLine(position, editor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this method should be able to become an instance method or getter.

editor being passed to atPoint is perhaps in error. It seems like the only thing read off from the editor is renderTree and element, and the root element can be read off the renderTree just as well. Several other methods are coupled to the renderTree so we should likely stop from using editor if we can get by with just the render tree and references to the AST (section).

let isPostBoundary = position.isHeadOfPost();
if (isPostBoundary) {
return position;
}

let { node, offset } = findNodeForPosition(position);

let range = document.createRange();
range.setStart(node, offset);
range.setEnd(node, offset);

let { y, height } = range.getBoundingClientRect();
let { left } = position.section.renderNode.element.getBoundingClientRect();

return Position.atPoint(left, y + height / 2, editor);
}

static blankPosition() {
return new BlankPosition();
}
Expand Down