From fc318ec78b8298e8cc99c607624135e84d09f592 Mon Sep 17 00:00:00 2001 From: Samy Pesse Date: Mon, 17 Oct 2016 01:14:47 +0200 Subject: [PATCH] Add transform .unwrapInlineByKey with test and documentation --- docs/reference/models/transform.md | 6 ++++++ src/transforms/by-key.js | 17 +++++++++++++++++ src/transforms/index.js | 2 ++ .../by-key/unwrap-inline/single-block/index.js | 14 ++++++++++++++ .../unwrap-inline/single-block/input.yaml | 14 ++++++++++++++ .../unwrap-inline/single-block/output.yaml | 7 +++++++ 6 files changed, 60 insertions(+) create mode 100644 test/transforms/fixtures/by-key/unwrap-inline/single-block/index.js create mode 100644 test/transforms/fixtures/by-key/unwrap-inline/single-block/input.yaml create mode 100644 test/transforms/fixtures/by-key/unwrap-inline/single-block/output.yaml diff --git a/docs/reference/models/transform.md b/docs/reference/models/transform.md index 249e343c7..832574717 100644 --- a/docs/reference/models/transform.md +++ b/docs/reference/models/transform.md @@ -57,6 +57,7 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S - [`setMarkByKey`](#setmarkbykey) - [`setNodeByKey`](#setnodebykey) - [`splitNodeByKey`](#splitnodebykey) + - [`unwrapInlineByKey`](#unwrapinlinebykey) - [Document Transforms](#document-transforms) - [`deleteAtRange`](#deleteatrange) - [`deleteBackwardAtRange`](#deletebackwardatrange) @@ -317,6 +318,11 @@ Set a dictionary of `properties` on a [`Node`](./node.md) by its `key`. For conv Split a node by its `key` at an `offset`. +### `unwrapInlineByKey` +`unwrapInlineByKey(key: String, properties: Object) => Transform`
+`unwrapInlineByKey(key: String, type: String) => Transform` + +Unwrap all inner content of an [`Inline`](./inline.md) node that match `properties`. For convenience, you can pass a `type` string or `properties` object. ## Document Transforms diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index 90e346ad9..1ac192e6d 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -193,3 +193,20 @@ export function splitNodeByKey(transform, key, offset) { const path = document.getPath(key) return transform.splitNodeOperation(path, offset) } + +/** + * Unwrap content from an inline parent with `properties`. + * + * @param {Transform} transform + * @param {String} key + * @param {Object or String} properties + * @return {Transform} + */ + +export function unwrapInlineByKey(transform, key, properties) { + const { state } = transform + const { document, selection } = state + const node = document.assertDescendant(key) + const range = selection.moveToRangeOf(node) + return transform.unwrapInline(range, properties) +} diff --git a/src/transforms/index.js b/src/transforms/index.js index 8d750bd1e..d1990e1bc 100644 --- a/src/transforms/index.js +++ b/src/transforms/index.js @@ -92,6 +92,7 @@ import { setMarkByKey, setNodeByKey, splitNodeByKey, + unwrapInlineByKey } from './by-key' /** @@ -238,6 +239,7 @@ export default { setMarkByKey, setNodeByKey, splitNodeByKey, + unwrapInlineByKey, /** * On selection. diff --git a/test/transforms/fixtures/by-key/unwrap-inline/single-block/index.js b/test/transforms/fixtures/by-key/unwrap-inline/single-block/index.js new file mode 100644 index 000000000..150a8fcf8 --- /dev/null +++ b/test/transforms/fixtures/by-key/unwrap-inline/single-block/index.js @@ -0,0 +1,14 @@ + +import assert from 'assert' + +export default function (state) { + const { document, selection } = state + const inline = document.assertPath([0, 1]) + + const next = state + .transform() + .unwrapInlineByKey(inline.key, 'hashtag') + .apply() + + return next +} diff --git a/test/transforms/fixtures/by-key/unwrap-inline/single-block/input.yaml b/test/transforms/fixtures/by-key/unwrap-inline/single-block/input.yaml new file mode 100644 index 000000000..7bfbe8b4b --- /dev/null +++ b/test/transforms/fixtures/by-key/unwrap-inline/single-block/input.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: w + - kind: inline + type: hashtag + nodes: + - kind: text + text: or + - kind: text + text: d diff --git a/test/transforms/fixtures/by-key/unwrap-inline/single-block/output.yaml b/test/transforms/fixtures/by-key/unwrap-inline/single-block/output.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/by-key/unwrap-inline/single-block/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: word