prosemirror-transform
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -0,1 +1,9 @@ | ||
## 1.7.0 (2022-08-16) | ||
### New features | ||
The new `AttrStep` (and `Transform.setNodeAttribute`) can be used to set individual attributes on a node. | ||
`AddNodeMarkStep` and `RemoveNodeMarkStep` can now be used to add and remove marks on individual nodes. `Transform.addNodeMark`/`removeNodeMark` provide an interface to these in transform objects. | ||
## 1.6.0 (2022-06-01) | ||
@@ -2,0 +10,0 @@ |
@@ -416,2 +416,15 @@ import { Node, Schema, Slice, Fragment, NodeRange, NodeType, Attrs, Mark, MarkType, ContentMatch } from 'prosemirror-model'; | ||
/** | ||
Set a single attribute on a given node to a new value. | ||
*/ | ||
setNodeAttribute(pos: number, attr: string, value: any): this; | ||
/** | ||
Add a mark to the node at position `pos`. | ||
*/ | ||
addNodeMark(pos: number, mark: Mark): this; | ||
/** | ||
Remove a mark (or a mark of the given type) from the node at | ||
position `pos`. | ||
*/ | ||
removeNodeMark(pos: number, mark: Mark | MarkType): this; | ||
/** | ||
Split the node at the given position, and optionally, if `depth` is | ||
@@ -574,2 +587,60 @@ greater than one, any number of nodes above that. By default, the | ||
} | ||
/** | ||
Add a mark to a specific node. | ||
*/ | ||
declare class AddNodeMarkStep extends Step { | ||
/** | ||
The position of the target node. | ||
*/ | ||
readonly pos: number; | ||
/** | ||
The mark to add. | ||
*/ | ||
readonly mark: Mark; | ||
/** | ||
Create a node mark step. | ||
*/ | ||
constructor( | ||
/** | ||
The position of the target node. | ||
*/ | ||
pos: number, | ||
/** | ||
The mark to add. | ||
*/ | ||
mark: Mark); | ||
apply(doc: Node): StepResult; | ||
invert(doc: Node): Step; | ||
map(mapping: Mappable): Step | null; | ||
toJSON(): any; | ||
} | ||
/** | ||
Remove a mark from a specific node. | ||
*/ | ||
declare class RemoveNodeMarkStep extends Step { | ||
/** | ||
The position of the target node. | ||
*/ | ||
readonly pos: number; | ||
/** | ||
The mark to remove. | ||
*/ | ||
readonly mark: Mark; | ||
/** | ||
Create a mark-removing step. | ||
*/ | ||
constructor( | ||
/** | ||
The position of the target node. | ||
*/ | ||
pos: number, | ||
/** | ||
The mark to remove. | ||
*/ | ||
mark: Mark); | ||
apply(doc: Node): StepResult; | ||
invert(doc: Node): Step; | ||
map(mapping: Mappable): Step | null; | ||
toJSON(): any; | ||
} | ||
@@ -700,2 +771,35 @@ /** | ||
/** | ||
Update an attribute in a specific node. | ||
*/ | ||
declare class AttrStep extends Step { | ||
/** | ||
The position of the target node. | ||
*/ | ||
readonly pos: number; | ||
/** | ||
The attribute to set. | ||
*/ | ||
readonly attr: string; | ||
readonly value: any; | ||
/** | ||
Construct an attribute step. | ||
*/ | ||
constructor( | ||
/** | ||
The position of the target node. | ||
*/ | ||
pos: number, | ||
/** | ||
The attribute to set. | ||
*/ | ||
attr: string, value: any); | ||
apply(doc: Node): StepResult; | ||
getMap(): StepMap; | ||
invert(doc: Node): AttrStep; | ||
map(mapping: Mappable): AttrStep | null; | ||
toJSON(): any; | ||
static fromJSON(schema: Schema, json: any): AttrStep; | ||
} | ||
/** | ||
‘Fit’ a slice into a given position in the document, producing a | ||
@@ -708,2 +812,2 @@ [step](https://prosemirror.net/docs/ref/#transform.Step) that inserts it. Will return null if | ||
export { AddMarkStep, MapResult, Mappable, Mapping, RemoveMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep }; | ||
export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep }; |
{ | ||
"name": "prosemirror-transform", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "ProseMirror document transformations", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -7,5 +7,6 @@ export {Transform} from "./transform" | ||
export {StepMap, MapResult, Mapping, Mappable} from "./map" | ||
export {AddMarkStep, RemoveMarkStep} from "./mark_step" | ||
export {AddMarkStep, RemoveMarkStep, AddNodeMarkStep, RemoveNodeMarkStep} from "./mark_step" | ||
export {ReplaceStep, ReplaceAroundStep} from "./replace_step" | ||
export {AttrStep} from "./attr_step" | ||
import "./mark" | ||
export {replaceStep} from "./replace" |
@@ -129,1 +129,97 @@ import {Fragment, Slice, Node, Mark, Schema} from "prosemirror-model" | ||
Step.jsonID("removeMark", RemoveMarkStep) | ||
/// Add a mark to a specific node. | ||
export class AddNodeMarkStep extends Step { | ||
/// Create a node mark step. | ||
constructor( | ||
/// The position of the target node. | ||
readonly pos: number, | ||
/// The mark to add. | ||
readonly mark: Mark | ||
) { | ||
super() | ||
} | ||
apply(doc: Node) { | ||
let node = doc.nodeAt(this.pos) | ||
if (!node) return StepResult.fail("No node at mark step's position") | ||
let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks)) | ||
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1)) | ||
} | ||
invert(doc: Node): Step { | ||
let node = doc.nodeAt(this.pos) | ||
if (node) { | ||
let newSet = this.mark.addToSet(node.marks) | ||
if (newSet.length == node.marks.length) { | ||
for (let i = 0; i < node.marks.length; i++) | ||
if (!node.marks[i].isInSet(newSet)) | ||
return new AddNodeMarkStep(this.pos, node.marks[i]) | ||
return new AddNodeMarkStep(this.pos, this.mark) | ||
} | ||
} | ||
return new RemoveNodeMarkStep(this.pos, this.mark) | ||
} | ||
map(mapping: Mappable): Step | null { | ||
let pos = mapping.mapResult(this.pos, 1) | ||
return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark) | ||
} | ||
toJSON(): any { | ||
return {stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON()} | ||
} | ||
/// @internal | ||
static fromJSON(schema: Schema, json: any) { | ||
if (typeof json.pos != "number") | ||
throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON") | ||
return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark)) | ||
} | ||
} | ||
Step.jsonID("addNodeMark", AddNodeMarkStep) | ||
/// Remove a mark from a specific node. | ||
export class RemoveNodeMarkStep extends Step { | ||
/// Create a mark-removing step. | ||
constructor( | ||
/// The position of the target node. | ||
readonly pos: number, | ||
/// The mark to remove. | ||
readonly mark: Mark | ||
) { | ||
super() | ||
} | ||
apply(doc: Node) { | ||
let node = doc.nodeAt(this.pos) | ||
if (!node) return StepResult.fail("No node at mark step's position") | ||
let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks)) | ||
return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1)) | ||
} | ||
invert(doc: Node): Step { | ||
let node = doc.nodeAt(this.pos) | ||
if (!node || !this.mark.isInSet(node.marks)) return this | ||
return new AddNodeMarkStep(this.pos, this.mark) | ||
} | ||
map(mapping: Mappable): Step | null { | ||
let pos = mapping.mapResult(this.pos, 1) | ||
return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark) | ||
} | ||
toJSON(): any { | ||
return {stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON()} | ||
} | ||
/// @internal | ||
static fromJSON(schema: Schema, json: any) { | ||
if (typeof json.pos != "number") | ||
throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON") | ||
return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark)) | ||
} | ||
} | ||
Step.jsonID("removeNodeMark", RemoveNodeMarkStep) |
@@ -23,2 +23,5 @@ This module defines a way of modifying documents that allows changes | ||
@RemoveMarkStep | ||
@AddNodeMarkStep | ||
@RemoveNodeMarkStep | ||
@AttrStep | ||
@@ -25,0 +28,0 @@ ### Position Mapping |
@@ -8,2 +8,4 @@ import {Node, NodeType, Mark, MarkType, ContentMatch, Slice, Fragment, NodeRange, Attrs} from "prosemirror-model" | ||
import {lift, wrap, setBlockType, setNodeMarkup, split, join} from "./structure" | ||
import {AttrStep} from "./attr_step" | ||
import {AddNodeMarkStep, RemoveNodeMarkStep} from "./mark_step" | ||
@@ -180,2 +182,27 @@ /// @internal | ||
/// Set a single attribute on a given node to a new value. | ||
setNodeAttribute(pos: number, attr: string, value: any): this { | ||
this.step(new AttrStep(pos, attr, value)) | ||
return this | ||
} | ||
/// Add a mark to the node at position `pos`. | ||
addNodeMark(pos: number, mark: Mark): this { | ||
this.step(new AddNodeMarkStep(pos, mark)) | ||
return this | ||
} | ||
/// Remove a mark (or a mark of the given type) from the node at | ||
/// position `pos`. | ||
removeNodeMark(pos: number, mark: Mark | MarkType): this { | ||
if (!(mark instanceof Mark)) { | ||
let node = this.doc.nodeAt(pos) | ||
if (!node) throw new RangeError("No node at position " + pos) | ||
mark = mark.isInSet(node.marks)! | ||
if (!mark) return this | ||
} | ||
this.step(new RemoveNodeMarkStep(pos, mark)) | ||
return this | ||
} | ||
/// Split the node at the given position, and optionally, if `depth` is | ||
@@ -182,0 +209,0 @@ /// greater than one, any number of nodes above that. By default, the |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
269485
20
6219