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

Writing Flow: Improve emulated caret positioning #5808

Merged
merged 3 commits into from
Apr 11, 2018
Merged
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
24 changes: 5 additions & 19 deletions editor/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ import {
isInSameBlock,
} from '../../utils/dom';

/**
* Browser dependencies
*/

const { DOMRect } = window;

/**
* Module Constants
*/

Copy link
Member

Choose a reason for hiding this comment

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

Some remains form previous work?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it intentionally, only because it's meant as a section marker, not a JSDoc for the variables being assigned. Definitely not related to the pull request, but I thought I could sneak it past. Apparently not without being noticed 😄

Copy link
Member

@ellatrix ellatrix Apr 11, 2018

Choose a reason for hiding this comment

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

Oh!

Isn't it common to do this then?

/*
 * Module Constants
 */
const { UP, DOWN, LEFT, RIGHT } = keycodes;

https://make.wordpress.org/core/handbook/best-practices/inline-documentation-standards/javascript/#multi-line-comments

Copy link
Member

Choose a reason for hiding this comment

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

Which we could also use for the "dependencies" comments.

Copy link
Member Author

@aduth aduth Apr 11, 2018

Choose a reason for hiding this comment

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

Technically, yes (though in the specific example, "Module constants" is prone to laziness and ought to at least be accompanied by variable-specific JSDoc).

I was going to make a big argument about how I disagree with the double-vs-single asterisk, but I honestly don't care too much†. There is quite a bit of existing code which would need to be refactored.

Where I might disagree and champion for discussion is some implication from the standards that multi-line comments where each line is prefixed with // are not valid for inline code. Aside from being far more verbose, I find it hard to disambiguate between a comment and a JSDoc, where only the latter associates a relationship to the immediate following line (not lines, as in a comment describing a sequence of logic), tying back to good code commenting habits of commenting the "what" (JSDoc) from the "why"/"how" (logic flow comments).

...Aside from my own personal-workflow incompatibility for me in that Docblock completion via DocBlockr extension only automates creation of intermediate-line asterisks for the double asterisk.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I don't care that much either. :)

Copy link
Member

Choose a reason for hiding this comment

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

Was just wondering.

const { UP, DOWN, LEFT, RIGHT } = keycodes;

/**
Expand Down Expand Up @@ -228,23 +223,14 @@ class WritingFlow extends Component {
}

/**
* Shifts focus to the last tabbable text field — if one exists — at the
* given mouse event's X coordinate.
*
* @param {MouseEvent} event Mouse event to align caret X offset.
* Sets focus to the end of the last tabbable text field, if one exists.
*/
focusLastTextField( event ) {
focusLastTextField() {
const focusableNodes = focus.focusable.find( this.container );
const target = findLast( focusableNodes, isTabbableTextField );
if ( ! target ) {
return;
if ( target ) {
placeCaretAtHorizontalEdge( target, true );
}

// Emulate a rect at which caret should be placed using mouse event.
const rect = target.getBoundingClientRect();
const targetRect = new DOMRect( event.clientX, rect.top, 0, rect.height );

placeCaretAtVerticalEdge( target, false, targetRect );
}

render() {
Expand Down
6 changes: 6 additions & 0 deletions utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ export function placeCaretAtVerticalEdge( container, isReverse, rect, mayUseScro
return;
}

// Offset by a buffer half the height of the caret rect. This is needed
// because caretRangeFromPoint may default to the end of the selection if
// offset is too close to the edge. It's unclear how to precisely calculate
// this threshold; it may be the padded area of some combination of line
// height, caret height, and font size. The buffer offset is effectively
// equivalent to a point at half the height of a line of text.
const buffer = rect.height / 2;
Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I should have better commented this.

const editableRect = container.getBoundingClientRect();
const x = rect.left + ( rect.width / 2 );
Expand Down