prosemirror-transform
Advanced tools
Comparing version 1.7.5 to 1.8.0
@@ -0,1 +1,9 @@ | ||
## 1.8.0 (2023-10-01) | ||
### New features | ||
The new `DocAttrStep` can be used to set attributes on the document's top node. | ||
`Transform.setDocAttribute` can be used to create a `DocAttrStep` in a transform. | ||
## 1.7.5 (2023-08-22) | ||
@@ -2,0 +10,0 @@ |
@@ -417,5 +417,11 @@ 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. | ||
The `pos` addresses the document content. Use `setDocAttribute` | ||
to set attributes on the document itself. | ||
*/ | ||
setNodeAttribute(pos: number, attr: string, value: any): this; | ||
/** | ||
Set a single attribute on the document to a new value. | ||
*/ | ||
setDocAttribute(attr: string, value: any): this; | ||
/** | ||
Add a mark to the node at position `pos`. | ||
@@ -801,2 +807,26 @@ */ | ||
} | ||
/** | ||
Update an attribute in the doc node. | ||
*/ | ||
declare class DocAttrStep extends Step { | ||
/** | ||
The attribute to set. | ||
*/ | ||
readonly attr: string; | ||
readonly value: any; | ||
/** | ||
Construct an attribute step. | ||
*/ | ||
constructor( | ||
/** | ||
The attribute to set. | ||
*/ | ||
attr: string, value: any); | ||
apply(doc: Node): StepResult; | ||
getMap(): StepMap; | ||
invert(doc: Node): DocAttrStep; | ||
map(mapping: Mappable): this; | ||
toJSON(): any; | ||
static fromJSON(schema: Schema, json: any): DocAttrStep; | ||
} | ||
@@ -811,2 +841,2 @@ /** | ||
export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep }; | ||
export { AddMarkStep, AddNodeMarkStep, AttrStep, DocAttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep }; |
{ | ||
"name": "prosemirror-transform", | ||
"version": "1.7.5", | ||
"version": "1.8.0", | ||
"description": "ProseMirror document transformations", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -54,1 +54,46 @@ import {Fragment, Slice, Node, Schema} from "prosemirror-model" | ||
Step.jsonID("attr", AttrStep) | ||
/// Update an attribute in the doc node. | ||
export class DocAttrStep extends Step { | ||
/// Construct an attribute step. | ||
constructor( | ||
/// The attribute to set. | ||
readonly attr: string, | ||
// The attribute's new value. | ||
readonly value: any | ||
) { | ||
super() | ||
} | ||
apply(doc: Node) { | ||
let attrs = Object.create(null) | ||
for (let name in doc.attrs) attrs[name] = doc.attrs[name] | ||
attrs[this.attr] = this.value | ||
let updated = doc.type.create(attrs, doc.content, doc.marks) | ||
return StepResult.ok(updated) | ||
} | ||
getMap() { | ||
return StepMap.empty | ||
} | ||
invert(doc: Node) { | ||
return new DocAttrStep(this.attr, doc.attrs[this.attr]) | ||
} | ||
map(mapping: Mappable) { | ||
return this | ||
} | ||
toJSON(): any { | ||
return {stepType: "docAttr", attr: this.attr, value: this.value} | ||
} | ||
static fromJSON(schema: Schema, json: any) { | ||
if (typeof json.attr != "string") | ||
throw new RangeError("Invalid input for DocAttrStep.fromJSON") | ||
return new DocAttrStep(json.attr, json.value) | ||
} | ||
} | ||
Step.jsonID("docAttr", DocAttrStep) |
@@ -9,4 +9,4 @@ export {Transform} from "./transform" | ||
export {ReplaceStep, ReplaceAroundStep} from "./replace_step" | ||
export {AttrStep} from "./attr_step" | ||
export {AttrStep, DocAttrStep} from "./attr_step" | ||
import "./mark" | ||
export {replaceStep} from "./replace" |
@@ -26,2 +26,3 @@ This module defines a way of modifying documents that allows changes | ||
@AttrStep | ||
@DocAttrStep | ||
@@ -28,0 +29,0 @@ ### Position Mapping |
@@ -8,3 +8,3 @@ 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 {AttrStep, DocAttrStep} from "./attr_step" | ||
import {AddNodeMarkStep, RemoveNodeMarkStep} from "./mark_step" | ||
@@ -183,2 +183,4 @@ | ||
/// Set a single attribute on a given node to a new value. | ||
/// The `pos` addresses the document content. Use `setDocAttribute` | ||
/// to set attributes on the document itself. | ||
setNodeAttribute(pos: number, attr: string, value: any): this { | ||
@@ -189,2 +191,8 @@ this.step(new AttrStep(pos, attr, value)) | ||
/// Set a single attribute on the document to a new value. | ||
setDocAttribute(attr: string, value: any): this { | ||
this.step(new DocAttrStep(attr, value)) | ||
return this | ||
} | ||
/// Add a mark to the node at position `pos`. | ||
@@ -191,0 +199,0 @@ addNodeMark(pos: number, mark: Mark): this { |
Sorry, the diff of this file is not supported yet
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
304434
6467