prosemirror-transform
Advanced tools
Comparing version 0.17.0 to 0.18.0
@@ -213,2 +213,21 @@ // Mappable:: interface | ||
// :: (Mapping) | ||
// Append the inverse of the given mapping to this one. | ||
Mapping.prototype.appendMappingInverted = function appendMappingInverted (mapping) { | ||
var this$1 = this; | ||
for (var i = mapping.maps.length - 1, totalSize = this.maps.length + mapping.maps.length; i >= 0; i--) { | ||
var mirr = mapping.getMirror(i) | ||
this$1.appendMap(mapping.maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : null) | ||
} | ||
}; | ||
// () → Mapping | ||
// Create an inverted version of this mapping. | ||
Mapping.prototype.invert = function invert () { | ||
var inverse = new Mapping | ||
inverse.appendMappingInverted(this) | ||
return inverse | ||
}; | ||
// :: (number, ?number) → number | ||
@@ -215,0 +234,0 @@ // Map a position through this remapping. |
@@ -62,2 +62,6 @@ var ref = require("prosemirror-model"); | ||
AddMarkStep.prototype.offset = function offset (n) { | ||
return new AddMarkStep(this.from + n, this.to + n, this.mark) | ||
}; | ||
AddMarkStep.fromJSON = function fromJSON (schema, json) { | ||
@@ -114,2 +118,6 @@ return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark)) | ||
RemoveMarkStep.prototype.offset = function offset (n) { | ||
return new RemoveMarkStep(this.from + n, this.to + n, this.mark) | ||
}; | ||
RemoveMarkStep.fromJSON = function fromJSON (schema, json) { | ||
@@ -116,0 +124,0 @@ return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark)) |
@@ -67,2 +67,6 @@ var ref = require("prosemirror-model"); | ||
ReplaceStep.prototype.offset = function offset (n) { | ||
return new ReplaceStep(this.from + n, this.to + n, this.slice, this.structure) | ||
}; | ||
ReplaceStep.fromJSON = function fromJSON (schema, json) { | ||
@@ -137,2 +141,7 @@ return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure) | ||
ReplaceAroundStep.prototype.offset = function offset (n) { | ||
return new ReplaceAroundStep(this.from + n, this.to + n, this.gapFrom + n, this.gapTo + n, | ||
this.slice, this.insert, this.structure) | ||
}; | ||
ReplaceAroundStep.fromJSON = function fromJSON (schema, json) { | ||
@@ -139,0 +148,0 @@ return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, |
@@ -34,7 +34,8 @@ var ref = require("prosemirror-model"); | ||
var $from = this.doc.resolve(from) | ||
if (fitsTrivially($from, this.doc.resolve(to), slice)) | ||
var $from = this.doc.resolve(from), $to = this.doc.resolve(to) | ||
if (fitsTrivially($from, $to, slice)) | ||
{ return this.step(new ReplaceStep(from, to, slice)) } | ||
var canExpand = coveredDepths($from, this.doc.resolve(to)), preferredExpand = 0 | ||
if (canExpand[canExpand.length - 1] == 0) { canExpand.pop() } | ||
canExpand.unshift($from.depth + 1) | ||
@@ -71,3 +72,3 @@ for (var d = $from.depth; d > 0; d--) { | ||
if (parent.canReplaceWith(index, index, insert.type, insert.attrs, insert.marks)) | ||
{ return this$1.replace($from.before(expandDepth), expandDepth > $from.depth ? to : $from.after(expandDepth), | ||
{ return this$1.replace($from.before(expandDepth), expandDepth > $from.depth ? to : $to.after(expandDepth), | ||
new Slice(closeFragment(slice.content, 0, slice.openLeft, openDepth), | ||
@@ -108,24 +109,18 @@ openDepth, slice.openRight)) } | ||
// :: (number, number) → Transform | ||
// Delete the given range, and any fully covered parent nodes that are | ||
// not allowed to be empty. | ||
// Delete the given range, expanding it to cover fully covered | ||
// parent nodes until a valid replace is found. | ||
Transform.prototype.deleteRange = function(from, to) { | ||
var $from = this.doc.resolve(from) | ||
var covered = coveredDepths($from, this.doc.resolve(to)), grown = false | ||
// Find the innermost covered node that allows its whole content to | ||
// be deleted | ||
var $from = this.doc.resolve(from), $to = this.doc.resolve(to) | ||
var covered = coveredDepths($from, $to) | ||
for (var i = 0; i < covered.length; i++) { | ||
if ($from.node(covered[i]).contentMatchAt(0).validEnd()) { | ||
from = $from.start(covered[i]) | ||
to = $from.end(covered[i]) | ||
grown = true | ||
var depth = covered[i], last = i == covered.length - 1 | ||
if ((last && depth == 0) || $from.node(depth).contentMatchAt(0).validEnd()) { | ||
from = $from.start(depth) | ||
to = $to.end(depth) | ||
break | ||
} | ||
} | ||
// If no such node was found and the outermose covered node can be | ||
// deleted entirely, do that | ||
if (!grown && covered.length) { | ||
var depth = covered[covered.length - 1] | ||
if ($from.node(depth - 1).canReplace($from.index(depth - 1), $from.indexAfter(depth - 1))) { | ||
if (depth > 0 && (last || $from.node(depth - 1).canReplace($from.index(depth - 1), $to.indexAfter(depth - 1)))) { | ||
from = $from.before(depth) | ||
to = $from.after(depth) | ||
to = $to.after(depth) | ||
break | ||
} | ||
@@ -138,9 +133,10 @@ } | ||
// Returns an array of all depths for which $from - $to spans the | ||
// whole content of the node at that depth. | ||
// whole content of the nodes at that depth. | ||
function coveredDepths($from, $to) { | ||
var result = [] | ||
for (var i = 0; i < $from.depth; i++) { | ||
var depth = $from.depth - i | ||
if ($from.pos - i > $from.start(depth)) { break } | ||
if ($to.depth >= depth && $to.pos + ($to.depth - depth) == $from.end(depth)) { result.push(depth) } | ||
var result = [], minDepth = Math.min($from.depth, $to.depth) | ||
for (var d = minDepth; d >= 0; d--) { | ||
var start = $from.start(d) | ||
if (start < $from.pos - ($from.depth - d) || | ||
$to.end(d) > $to.pos + ($to.depth - d)) { break } | ||
if (start == $to.start(d)) { result.push(d) } | ||
} | ||
@@ -147,0 +143,0 @@ return result |
@@ -46,2 +46,9 @@ var ref = require("prosemirror-model"); | ||
// :: (n: number) → Step | ||
// Returns a copy of this step in which all positions have `n` added | ||
// to them. The main use for this is to take a step in one document, | ||
// and make it apply to a sub-document, or a larger document that | ||
// the original document is a part of. | ||
Step.prototype.offset = function offset (_n) { return mustOverride() }; | ||
// :: () → Object | ||
@@ -48,0 +55,0 @@ // Create a JSON-serializeable representation of this step. By |
@@ -148,10 +148,11 @@ var ref = require("prosemirror-model"); | ||
// :: (number, ?NodeType, ?Object) → Transform | ||
// :: (number, ?NodeType, ?Object, ?[Mark]) → Transform | ||
// Change the type and attributes of the node after `pos`. | ||
Transform.prototype.setNodeType = function(pos, type, attrs) { | ||
Transform.prototype.setNodeType = function(pos, type, attrs, marks) { | ||
var node = this.doc.nodeAt(pos) | ||
if (!node) { throw new RangeError("No node at given position") } | ||
if (!type) { type = node.type } | ||
var newNode = type.create(attrs, null, marks || node.marks) | ||
if (node.isLeaf) | ||
{ return this.replaceWith(pos, pos + node.nodeSize, type.create(attrs, null, node.marks)) } | ||
{ return this.replaceWith(pos, pos + node.nodeSize, newNode) } | ||
@@ -162,3 +163,3 @@ if (!type.validContent(node.content, attrs)) | ||
return this.step(new ReplaceAroundStep(pos, pos + node.nodeSize, pos + 1, pos + node.nodeSize - 1, | ||
new Slice(Fragment.from(type.create(attrs)), 0, 0), 1, true)) | ||
new Slice(Fragment.from(newNode), 0, 0), 1, true)) | ||
} | ||
@@ -165,0 +166,0 @@ |
@@ -5,10 +5,13 @@ var ref = require("./map"); | ||
var TransformError = (function (Error) { | ||
function TransformError () { | ||
Error.apply(this, arguments); | ||
}if ( Error ) TransformError.__proto__ = Error; | ||
function TransformError(message) { Error.call(this, message) } | ||
if ( Error ) TransformError.__proto__ = Error; | ||
TransformError.prototype = Object.create( Error && Error.prototype ); | ||
TransformError.prototype.constructor = TransformError; | ||
var prototypeAccessors = { name: {} }; | ||
prototypeAccessors.name.get = function () { return "TransformError" }; | ||
Object.defineProperties( TransformError.prototype, prototypeAccessors ); | ||
return TransformError; | ||
@@ -39,6 +42,6 @@ }(Error)); | ||
var prototypeAccessors = { before: {} }; | ||
var prototypeAccessors$1 = { before: {},docChanged: {} }; | ||
// :: Node The document at the start of the transformation. | ||
prototypeAccessors.before.get = function () { return this.docs.length ? this.docs[0] : this.doc }; | ||
prototypeAccessors$1.before.get = function () { return this.docs.length ? this.docs[0] : this.doc }; | ||
@@ -63,2 +66,8 @@ // :: (step: Step) → Transform | ||
// :: bool | ||
// True when this transaction changes the document. | ||
prototypeAccessors$1.docChanged.get = function () { | ||
return this.steps.length > 0 | ||
}; | ||
Transform.prototype.addStep = function addStep (step, doc) { | ||
@@ -71,3 +80,3 @@ this.docs.push(this.doc) | ||
Object.defineProperties( Transform.prototype, prototypeAccessors ); | ||
Object.defineProperties( Transform.prototype, prototypeAccessors$1 ); | ||
exports.Transform = Transform |
{ | ||
"name": "prosemirror-transform", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"description": "ProseMirror document transformations", | ||
@@ -19,3 +19,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"prosemirror-model": "^0.17.0" | ||
"prosemirror-model": "^0.18.0" | ||
}, | ||
@@ -22,0 +22,0 @@ "devDependencies": { |
@@ -204,2 +204,19 @@ // Mappable:: interface | ||
// :: (Mapping) | ||
// Append the inverse of the given mapping to this one. | ||
appendMappingInverted(mapping) { | ||
for (let i = mapping.maps.length - 1, totalSize = this.maps.length + mapping.maps.length; i >= 0; i--) { | ||
let mirr = mapping.getMirror(i) | ||
this.appendMap(mapping.maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : null) | ||
} | ||
} | ||
// () → Mapping | ||
// Create an inverted version of this mapping. | ||
invert() { | ||
let inverse = new Mapping | ||
inverse.appendMappingInverted(this) | ||
return inverse | ||
} | ||
// :: (number, ?number) → number | ||
@@ -206,0 +223,0 @@ // Map a position through this remapping. |
@@ -53,2 +53,6 @@ const {Fragment, Slice} = require("prosemirror-model") | ||
offset(n) { | ||
return new AddMarkStep(this.from + n, this.to + n, this.mark) | ||
} | ||
static fromJSON(schema, json) { | ||
@@ -98,2 +102,6 @@ return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark)) | ||
offset(n) { | ||
return new RemoveMarkStep(this.from + n, this.to + n, this.mark) | ||
} | ||
static fromJSON(schema, json) { | ||
@@ -100,0 +108,0 @@ return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark)) |
@@ -8,3 +8,3 @@ const {Slice} = require("prosemirror-model") | ||
class ReplaceStep extends Step { | ||
// :: (number, number, Slice, bool) | ||
// :: (number, number, Slice, ?bool) | ||
// The given `slice` should fit the 'gap' between `from` and | ||
@@ -68,2 +68,6 @@ // `to`—the depths must line up, and the surrounding nodes must be | ||
offset(n) { | ||
return new ReplaceStep(this.from + n, this.to + n, this.slice, this.structure) | ||
} | ||
static fromJSON(schema, json) { | ||
@@ -81,3 +85,3 @@ return new ReplaceStep(json.from, json.to, Slice.fromJSON(schema, json.slice), !!json.structure) | ||
class ReplaceAroundStep extends Step { | ||
// :: (number, number, number, number, Slice, number, bool) | ||
// :: (number, number, number, number, Slice, number, ?bool) | ||
// Create a replace-wrap step with the given range and gap. `insert` | ||
@@ -138,2 +142,7 @@ // should be the point in the slice into which the gap should be | ||
offset(n) { | ||
return new ReplaceAroundStep(this.from + n, this.to + n, this.gapFrom + n, this.gapTo + n, | ||
this.slice, this.insert, this.structure) | ||
} | ||
static fromJSON(schema, json) { | ||
@@ -140,0 +149,0 @@ return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, |
@@ -26,7 +26,8 @@ const {Fragment, Slice} = require("prosemirror-model") | ||
let $from = this.doc.resolve(from) | ||
if (fitsTrivially($from, this.doc.resolve(to), slice)) | ||
let $from = this.doc.resolve(from), $to = this.doc.resolve(to) | ||
if (fitsTrivially($from, $to, slice)) | ||
return this.step(new ReplaceStep(from, to, slice)) | ||
let canExpand = coveredDepths($from, this.doc.resolve(to)), preferredExpand = 0 | ||
if (canExpand[canExpand.length - 1] == 0) canExpand.pop() | ||
canExpand.unshift($from.depth + 1) | ||
@@ -63,3 +64,3 @@ for (let d = $from.depth; d > 0; d--) { | ||
if (parent.canReplaceWith(index, index, insert.type, insert.attrs, insert.marks)) | ||
return this.replace($from.before(expandDepth), expandDepth > $from.depth ? to : $from.after(expandDepth), | ||
return this.replace($from.before(expandDepth), expandDepth > $from.depth ? to : $to.after(expandDepth), | ||
new Slice(closeFragment(slice.content, 0, slice.openLeft, openDepth), | ||
@@ -100,24 +101,18 @@ openDepth, slice.openRight)) | ||
// :: (number, number) → Transform | ||
// Delete the given range, and any fully covered parent nodes that are | ||
// not allowed to be empty. | ||
// Delete the given range, expanding it to cover fully covered | ||
// parent nodes until a valid replace is found. | ||
Transform.prototype.deleteRange = function(from, to) { | ||
let $from = this.doc.resolve(from) | ||
let covered = coveredDepths($from, this.doc.resolve(to)), grown = false | ||
// Find the innermost covered node that allows its whole content to | ||
// be deleted | ||
let $from = this.doc.resolve(from), $to = this.doc.resolve(to) | ||
let covered = coveredDepths($from, $to) | ||
for (let i = 0; i < covered.length; i++) { | ||
if ($from.node(covered[i]).contentMatchAt(0).validEnd()) { | ||
from = $from.start(covered[i]) | ||
to = $from.end(covered[i]) | ||
grown = true | ||
let depth = covered[i], last = i == covered.length - 1 | ||
if ((last && depth == 0) || $from.node(depth).contentMatchAt(0).validEnd()) { | ||
from = $from.start(depth) | ||
to = $to.end(depth) | ||
break | ||
} | ||
} | ||
// If no such node was found and the outermose covered node can be | ||
// deleted entirely, do that | ||
if (!grown && covered.length) { | ||
let depth = covered[covered.length - 1] | ||
if ($from.node(depth - 1).canReplace($from.index(depth - 1), $from.indexAfter(depth - 1))) { | ||
if (depth > 0 && (last || $from.node(depth - 1).canReplace($from.index(depth - 1), $to.indexAfter(depth - 1)))) { | ||
from = $from.before(depth) | ||
to = $from.after(depth) | ||
to = $to.after(depth) | ||
break | ||
} | ||
@@ -130,9 +125,10 @@ } | ||
// Returns an array of all depths for which $from - $to spans the | ||
// whole content of the node at that depth. | ||
// whole content of the nodes at that depth. | ||
function coveredDepths($from, $to) { | ||
let result = [] | ||
for (let i = 0; i < $from.depth; i++) { | ||
let depth = $from.depth - i | ||
if ($from.pos - i > $from.start(depth)) break | ||
if ($to.depth >= depth && $to.pos + ($to.depth - depth) == $from.end(depth)) result.push(depth) | ||
let result = [], minDepth = Math.min($from.depth, $to.depth) | ||
for (let d = minDepth; d >= 0; d--) { | ||
let start = $from.start(d) | ||
if (start < $from.pos - ($from.depth - d) || | ||
$to.end(d) > $to.pos + ($to.depth - d)) break | ||
if (start == $to.start(d)) result.push(d) | ||
} | ||
@@ -139,0 +135,0 @@ return result |
@@ -48,2 +48,9 @@ const {ReplaceError} = require("prosemirror-model") | ||
// :: (n: number) → Step | ||
// Returns a copy of this step in which all positions have `n` added | ||
// to them. The main use for this is to take a step in one document, | ||
// and make it apply to a sub-document, or a larger document that | ||
// the original document is a part of. | ||
offset(_n) { return mustOverride() } | ||
// :: () → Object | ||
@@ -50,0 +57,0 @@ // Create a JSON-serializeable representation of this step. By |
@@ -132,10 +132,11 @@ const {Slice, Fragment} = require("prosemirror-model") | ||
// :: (number, ?NodeType, ?Object) → Transform | ||
// :: (number, ?NodeType, ?Object, ?[Mark]) → Transform | ||
// Change the type and attributes of the node after `pos`. | ||
Transform.prototype.setNodeType = function(pos, type, attrs) { | ||
Transform.prototype.setNodeType = function(pos, type, attrs, marks) { | ||
let node = this.doc.nodeAt(pos) | ||
if (!node) throw new RangeError("No node at given position") | ||
if (!type) type = node.type | ||
let newNode = type.create(attrs, null, marks || node.marks) | ||
if (node.isLeaf) | ||
return this.replaceWith(pos, pos + node.nodeSize, type.create(attrs, null, node.marks)) | ||
return this.replaceWith(pos, pos + node.nodeSize, newNode) | ||
@@ -146,3 +147,3 @@ if (!type.validContent(node.content, attrs)) | ||
return this.step(new ReplaceAroundStep(pos, pos + node.nodeSize, pos + 1, pos + node.nodeSize - 1, | ||
new Slice(Fragment.from(type.create(attrs)), 0, 0), 1, true)) | ||
new Slice(Fragment.from(newNode), 0, 0), 1, true)) | ||
} | ||
@@ -149,0 +150,0 @@ |
const {Mapping} = require("./map") | ||
class TransformError extends Error {} | ||
class TransformError extends Error { | ||
constructor(message) { super(message) } | ||
get name() { return "TransformError" } | ||
} | ||
exports.TransformError = TransformError | ||
@@ -51,2 +54,8 @@ | ||
// :: bool | ||
// True when this transaction changes the document. | ||
get docChanged() { | ||
return this.steps.length > 0 | ||
} | ||
addStep(step, doc) { | ||
@@ -53,0 +62,0 @@ this.docs.push(this.doc) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134945
2872
+ Addedprosemirror-model@0.18.0(transitive)
- Removedprosemirror-model@0.17.0(transitive)
Updatedprosemirror-model@^0.18.0