Skip to content

Commit

Permalink
Merge pull request #93 from mixonic/click-card
Browse files Browse the repository at this point in the history
Fix clicking when there is no active cursor.
  • Loading branch information
bantic committed Aug 26, 2015
2 parents 7d93666 + 48d372e commit 3495f71
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 168 deletions.
2 changes: 1 addition & 1 deletion src/js/commands/bold.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class BoldCommand extends TextFormatCommand {
}
exec() {
let markerRange = this.editor.cursor.offsets;
if (!markerRange.leftRenderNode || !markerRange.rightRenderNode) {
if (!markerRange.headSection || !markerRange.tailSection) {
return;
}
let markers = this.editor.run((postEditor) => {
Expand Down
2 changes: 1 addition & 1 deletion src/js/commands/italic.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class ItalicCommand extends TextFormatCommand {
}
exec() {
let markerRange = this.editor.cursor.offsets;
if (!markerRange.leftRenderNode || !markerRange.rightRenderNode) {
if (!markerRange.headSection || !markerRange.tailSection) {
return;
}
let markers = this.editor.run((postEditor) => {
Expand Down
22 changes: 12 additions & 10 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
import { getData, setData } from '../utils/element-utils';
import mixin from '../utils/mixin';
import EventListenerMixin from '../utils/event-listener';
import Cursor from '../models/cursor';
import Cursor from '../utils/cursor';
import PostNodeBuilder from '../models/post-node-builder';

export const EDITOR_ELEMENT_CLASS_NAME = 'ck-editor';
Expand Down Expand Up @@ -338,10 +338,10 @@ class Editor {
this.cursor.moveToSection(offsets.headSection, offsets.headSectionOffset);
} else {
let results = this.run(postEditor => {
const {headMarker, headOffset} = offsets;
const {headMarker, headMarkerOffset} = offsets;
const key = Key.fromEvent(event);

const deletePosition = {marker: headMarker, offset: headOffset},
const deletePosition = {marker: headMarker, offset: headMarkerOffset},
direction = key.direction;
return postEditor.deleteFrom(deletePosition, direction);
});
Expand All @@ -354,7 +354,7 @@ class Editor {

// if there's no left/right nodes, we are probably not in the editor,
// or we have selected some non-marker thing like a card
if (!offsets.leftRenderNode || !offsets.rightRenderNode) {
if (!offsets.headSection || !offsets.tailSection) {
return;
}

Expand Down Expand Up @@ -533,16 +533,18 @@ class Editor {

get activeMarkers() {
const {
startMarker,
endMarker
headMarker,
tailMarker
} = this.cursor.offsets;

if (!(startMarker && endMarker)) {
return [];
let activeMarkers = [];

if (headMarker && tailMarker) {
this.post.markersFrom(headMarker, tailMarker, m => {
activeMarkers.push(m);
});
}

let activeMarkers = [];
this.post.markersFrom(startMarker, endMarker, m => activeMarkers.push(m));
return activeMarkers;
}

Expand Down
58 changes: 30 additions & 28 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class PostEditor {
* let marker = editor.post.sections.head.markers.head;
* editor.run((postEditor) => {
* postEditor.deleteRange({
* headMarker: marker,
* headOffset: 2,
* tailMarker: marker,
* tailOffset: 4,
* headSection: section,
* headSectionOffset: 2,
* tailSection: section,
* tailSectionOffset: 4,
* });
* });
*
* `deleteRange` accepts the value of `this.cursor.offsets` for deletion.
*
* @method deleteRange
* @param {Object} markerRange Object with offsets, {headMarker, headOffset, tailMarker, tailOffset}
* @param {Object} markerRange Object with offsets, {headSection, headSectionOffset, tailSection, tailSectionOffset}
* @return {Object} {currentMarker, currentOffset} for cursor
* @public
*/
Expand All @@ -56,8 +56,10 @@ class PostEditor {
// -- cursor goes at end of marker before the selection start

// markerRange should be akin to this.cursor.offset
const {headSection, headSectionOffset, tailSection, tailSectionOffset} = markerRange;
const {post} = this.editor;
const {
headSection, headSectionOffset, tailSection, tailSectionOffset
} = markerRange;
const { post } = this.editor;

if (headSection === tailSection) {
this.cutSection(headSection, headSectionOffset, tailSectionOffset);
Expand Down Expand Up @@ -88,9 +90,9 @@ class PostEditor {
}

cutSection(section, headSectionOffset, tailSectionOffset) {
const {marker:headMarker, offset:headOffset} = section.markerPositionAtOffset(headSectionOffset);
const {marker:tailMarker, offset:tailOffset} = section.markerPositionAtOffset(tailSectionOffset);
const markers = this.splitMarkers({headMarker, headOffset, tailMarker, tailOffset});
const {marker:headMarker, offset:headMarkerOffset} = section.markerPositionAtOffset(headSectionOffset);
const {marker:tailMarker, offset:tailMarkerOffset} = section.markerPositionAtOffset(tailSectionOffset);
const markers = this.splitMarkers({headMarker, headMarkerOffset, tailMarker, tailMarkerOffset});
section.markers.removeBy(m => {
return markers.indexOf(m) !== -1;
});
Expand Down Expand Up @@ -271,11 +273,11 @@ class PostEditor {
* provided. Markers on the outside of the split may also have been modified.
*
* @method splitMarkers
* @param {Object} markerRange Object with offsets, {headMarker, headOffset, tailMarker, tailOffset}
* @param {Object} markerRange Object with offsets, {headMarker, headMarkerOffset, tailMarker, tailMarkerOffset}
* @return {Array} of markers that are inside the split
* @public
*/
splitMarkers({headMarker, headOffset, tailMarker, tailOffset}) {
splitMarkers({headMarker, headMarkerOffset, tailMarker, tailMarkerOffset}) {
const { post } = this.editor;
let selectedMarkers = [];

Expand All @@ -291,27 +293,27 @@ class PostEditor {
tailMarker.section.renderNode.markDirty();

if (headMarker === tailMarker) {
let markers = headSection.splitMarker(headMarker, headOffset, tailOffset);
let markers = headSection.splitMarker(headMarker, headMarkerOffset, tailMarkerOffset);
selectedMarkers = post.markersInRange({
headMarker: markers[0],
tailMarker: markers[markers.length-1],
headOffset,
tailOffset
headMarkerOffset,
tailMarkerOffset
});
} else {
let newHeadMarkers = headSection.splitMarker(headMarker, headOffset);
let newHeadMarkers = headSection.splitMarker(headMarker, headMarkerOffset);
let selectedHeadMarkers = post.markersInRange({
headMarker: newHeadMarkers[0],
tailMarker: newHeadMarkers[newHeadMarkers.length-1],
headOffset
headMarkerOffset
});

let newTailMarkers = tailSection.splitMarker(tailMarker, 0, tailOffset);
let newTailMarkers = tailSection.splitMarker(tailMarker, 0, tailMarkerOffset);
let selectedTailMarkers = post.markersInRange({
headMarker: newTailMarkers[0],
tailMarker: newTailMarkers[newTailMarkers.length-1],
headOffset: 0,
tailOffset
headMarkerOffset: 0,
tailMarkerOffset
});

let newHeadMarker = selectedHeadMarkers[0],
Expand Down Expand Up @@ -348,8 +350,8 @@ class PostEditor {
* let marker = editor.post.sections.head.marker.head;
* editor.run((postEditor) => {
* postEditor.splitSection({
* headMarker: marker,
* headOffset: 3
* headSection: section,
* headSectionOffset: 3
* });
* });
* // Will result in the marker and its old section being removed from
Expand All @@ -358,20 +360,20 @@ class PostEditor {
*
* The return value will be the two new sections. One or both of these
* sections can be blank (contain only a blank marker), for example if the
* headOffset is 0.
* headMaOffset is 0.
*
* @method splitMarkers
* @param {Object} markerRange Object with offsets, {headMarker, headOffset, tailMarker, tailOffset}
* @param {Object} markerRange Object with offsets, {headSection, headSectionOffset}
* @return {Array} of new sections, one for the first half and one for the second
* @public
*/
splitSection({headSection: section, headSectionOffset}) {
let {
marker: headMarker,
offset: headOffset
offset: headMarkerOffset
} = section.markerPositionAtOffset(headSectionOffset);

const [beforeSection, afterSection] = section.splitAtMarker(headMarker, headOffset);
const [beforeSection, afterSection] = section.splitAtMarker(headMarker, headMarkerOffset);

this._replaceSection(section, [beforeSection, afterSection]);

Expand Down Expand Up @@ -406,7 +408,7 @@ class PostEditor {
* value as `splitMarkers`.
*
* @method applyMarkupToMarkers
* @param {Object} markerRange Object with offsets, {headMarker, headOffset, tailMarker, tailOffset}
* @param {Object} markerRange Object with offsets
* @param {Object} markup A markup post abstract node
* @return {Array} of markers that are inside the split
* @public
Expand Down Expand Up @@ -443,7 +445,7 @@ class PostEditor {
* value as `splitMarkers`.
*
* @method removeMarkupFromMarkers
* @param {Object} markerRange Object with offsets, {headMarker, headOffset, tailMarker, tailOffset}
* @param {Object} markerRange Object with offsets
* @param {Object} markup A markup post abstract node
* @return {Array} of markers that are inside the split
* @public
Expand Down
19 changes: 10 additions & 9 deletions src/js/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ export default class Post {
});
}

markersInRange({headMarker, headOffset, tailMarker, tailOffset}) {
markersInRange({headMarker, headMarkerOffset, tailMarker, tailMarkerOffset}) {
let offset = 0;
let foundMarkers = [];
let toEnd = tailOffset === undefined;
if (toEnd) { tailOffset = 0; }
let toEnd = tailMarkerOffset === undefined;
if (toEnd) { tailMarkerOffset = 0; }

this.markersFrom(headMarker, tailMarker, marker => {
if (toEnd) {
tailOffset += marker.length;
tailMarkerOffset += marker.length;
}

if (offset >= headOffset && offset < tailOffset) {
if (offset >= headMarkerOffset && offset < tailMarkerOffset) {
foundMarkers.push(marker);
}

Expand Down Expand Up @@ -74,20 +74,21 @@ export default class Post {
return {changedSections, removedSections};
}
/**
* Invoke `callbackFn` for all markers between the startMarker and endMarker (inclusive),
* Invoke `callbackFn` for all markers between the headMarker and tailMarker (inclusive),
* across sections
*/
markersFrom(startMarker, endMarker, callbackFn) {
let currentMarker = startMarker;
markersFrom(headMarker, tailMarker, callbackFn) {
let currentMarker = headMarker;
while (currentMarker) {
callbackFn(currentMarker);

if (currentMarker === endMarker) {
if (currentMarker === tailMarker) {
currentMarker = null;
} else if (currentMarker.next) {
currentMarker = currentMarker.next;
} else {
let nextSection = currentMarker.section.next;
// FIXME: This will fail across cards
currentMarker = nextSection && nextSection.markers.head;
}
}
Expand Down
Loading

0 comments on commit 3495f71

Please sign in to comment.