From 8b27e7e1844fe64df44c7d1816c711c7e074f619 Mon Sep 17 00:00:00 2001 From: Arrizal Amin Date: Thu, 25 Apr 2019 07:17:28 +0700 Subject: [PATCH] fix get marks from previous text on method getMarksAtPosition (#2688) --- packages/slate/src/interfaces/element.js | 2 +- ...-with-zero-offset-with-no-previous-text.js | 25 +++++++++++++++ ...ith-previous-text-not-in-the-same-block.js | 29 +++++++++++++++++ .../node/get-marks-at-position/marked-text.js | 28 ++++++++++++++++ .../text-with-zero-offset.js | 32 +++++++++++++++++++ .../get-marks-at-position/unmarked-text.js | 23 +++++++++++++ 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-no-previous-text.js create mode 100644 packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-previous-text-not-in-the-same-block.js create mode 100644 packages/slate/test/models/node/get-marks-at-position/marked-text.js create mode 100644 packages/slate/test/models/node/get-marks-at-position/text-with-zero-offset.js create mode 100644 packages/slate/test/models/node/get-marks-at-position/unmarked-text.js diff --git a/packages/slate/src/interfaces/element.js b/packages/slate/src/interfaces/element.js index 7ccff5e8f..0bccea955 100644 --- a/packages/slate/src/interfaces/element.js +++ b/packages/slate/src/interfaces/element.js @@ -973,7 +973,7 @@ class ElementInterface { const [previousText, previousPath] = previous if (closestBlock.hasDescendant(previousPath)) { - return previous.getMarksAtIndex(previousText.text.length) + return previousText.getMarksAtIndex(previousText.text.length) } return currentMarks diff --git a/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-no-previous-text.js b/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-no-previous-text.js new file mode 100644 index 000000000..f60ce87d6 --- /dev/null +++ b/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-no-previous-text.js @@ -0,0 +1,25 @@ +/** @jsx h */ + +import h from '../../../helpers/h' +import { Set } from 'immutable' +import PathUtils from '../../../../src/utils/path-utils' + +const path = PathUtils.create([0, 0]) + +export const input = ( + + + + + Cat is Cute + + + + +) + +export default function({ document, selection }) { + return document.getMarksAtPosition(path, 0) +} + +export const output = Set.of() diff --git a/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-previous-text-not-in-the-same-block.js b/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-previous-text-not-in-the-same-block.js new file mode 100644 index 000000000..3ecbc54ff --- /dev/null +++ b/packages/slate/test/models/node/get-marks-at-position/marked-text-with-zero-offset-with-previous-text-not-in-the-same-block.js @@ -0,0 +1,29 @@ +/** @jsx h */ + +import h from '../../../helpers/h' +import { Set } from 'immutable' +import { Mark } from 'slate' +import PathUtils from '../../../../src/utils/path-utils' + +const path = PathUtils.create([1, 0]) + +export const input = ( + + + + Cat is Cute + + + + Dog is Delightful + + + + +) + +export default function({ document, selection }) { + return document.getMarksAtPosition(path, 0) +} + +export const output = Set.of(Mark.create('bold')) diff --git a/packages/slate/test/models/node/get-marks-at-position/marked-text.js b/packages/slate/test/models/node/get-marks-at-position/marked-text.js new file mode 100644 index 000000000..ed8730c8c --- /dev/null +++ b/packages/slate/test/models/node/get-marks-at-position/marked-text.js @@ -0,0 +1,28 @@ +/** @jsx h */ + +import h from '../../../helpers/h' +import { Set } from 'immutable' +import { Mark } from 'slate' +import PathUtils from '../../../../src/utils/path-utils' + +const path = PathUtils.create([0, 0]) + +export const input = ( + + + + + Cat + is + Cute + + + + +) + +export default function({ document, selection }) { + return document.getMarksAtPosition(path, 1) +} + +export const output = Set.of(Mark.create('italic')) diff --git a/packages/slate/test/models/node/get-marks-at-position/text-with-zero-offset.js b/packages/slate/test/models/node/get-marks-at-position/text-with-zero-offset.js new file mode 100644 index 000000000..186704122 --- /dev/null +++ b/packages/slate/test/models/node/get-marks-at-position/text-with-zero-offset.js @@ -0,0 +1,32 @@ +/** @jsx h */ + +import h from '../../../helpers/h' +import { Set } from 'immutable' +import { Mark } from 'slate' +import PathUtils from '../../../../src/utils/path-utils' + +const path = PathUtils.create([1, 1, 0]) + +export const input = ( + + + + Cat is Cute + + + + Dog is + + + Delightful + + + + +) + +export default function({ document, selection }) { + return document.getMarksAtPosition(path, 0) +} + +export const output = Set.of(Mark.create('bold')) diff --git a/packages/slate/test/models/node/get-marks-at-position/unmarked-text.js b/packages/slate/test/models/node/get-marks-at-position/unmarked-text.js new file mode 100644 index 000000000..1bc1e448f --- /dev/null +++ b/packages/slate/test/models/node/get-marks-at-position/unmarked-text.js @@ -0,0 +1,23 @@ +/** @jsx h */ + +import h from '../../../helpers/h' +import { Set } from 'immutable' +import PathUtils from '../../../../src/utils/path-utils' + +const path = PathUtils.create([0, 0]) + +export const input = ( + + + + Cat is Cute + + + +) + +export default function({ document, selection }) { + return document.getMarksAtPosition(path, 1) +} + +export const output = Set.of()