@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
82 lines • 1.86 kB
JavaScript
import { StepMap } from 'prosemirror-transform';
export class TableSideEffectHandler {
constructor(tableSideEffect) {
if (tableSideEffect) {
this.table = tableSideEffect;
}
}
addTableSideEffect(from, to, node) {
this.table = {
from,
to,
node
};
}
handleAddTable(tr, isDelete) {
if (isDelete || !this.table) {
return false;
}
tr.insert(this.table.from, this.table.node);
return true;
}
handleRemoveTable(tr, tablePos, tableRect, column, isDelete) {
if (isDelete && tableRect.map.width === 1 && column === 0) {
// Add side effect
this.addTableSideEffect(tablePos, tablePos + tableRect.table.nodeSize, tableRect.table.copy(tableRect.table.content));
tr.delete(tablePos, tablePos + tableRect.table.nodeSize);
return true;
}
return false;
}
getTableMap(isDelete) {
if (!this.table) {
return;
}
const {
from,
to
} = this.table;
if (isDelete) {
return new StepMap([from, to - from, 0]);
}
return new StepMap([from, 0, to - from]);
}
map(mapping) {
if (!this.table) {
return;
}
return {
from: mapping.map(this.table.from),
to: mapping.map(this.table.to),
node: this.table.node
};
}
invert(doc) {
if (!this.table) {
return;
}
const tableNode = doc.nodeAt(this.table.from);
return {
from: this.table.from,
to: this.table.from + tableNode.nodeSize,
node: tableNode.copy(tableNode.content)
};
}
toJSON() {
if (!this.table) {
return;
}
return {
from: this.table.from,
to: this.table.to,
node: this.table.node.toJSON()
};
}
static fromJSON(schema, json) {
return {
from: json.from,
to: json.to,
node: schema.nodeFromJSON(json.node)
};
}
}