Text manipulation API to be used with macros
Opened this issue · 16 comments
Daniel wants to write a macro that manipulates text on the document. In order to not loose the annotations he'd have to use the TextOperation API.
@oliver---- What would he need to do in order to remove a certain word from all paragraphs, leaving the annotations intact?
Should we think about creating an abstraction API that makes such things easier?
@oliver----, I know how to iterate through each text node, but whet it comes to updating one text string... I don't know who to do it without annotations crushing.
This is now possible using transformations. https://github.com/substance/substance/tree/master/src/document/transformations
You can use the insertText transformation like so:
var sel = doc.createSelection({
type: 'property',
path: ['text1', 'content'],
startOffset: 10,
endOffset: 15
})
insertText(doc, {selection: sel, text: 'foo'});
insertText
replaces the text and takes care of annotations (the same is used when editing via UI)
... when you have a batch of changes on the same property, make sure to replace from right to left...
Then your computed offsets will still be valid after each single transformation.
Hmmm. But, probably you are not ready to switch to the new API...
Still... you can take a look at the impl of insertText to see how it works with the old API.
Annotation updates are done using https://github.com/substance/substance/blob/master/src/document/annotation_updates.js
(is also there in your version)
@oliver----, thanks!
Is it possible to use new api only on server side?
Yes, should be possible on server side.
Just be aware that you don't mix different substance versions. The Archivist-Composer needs to stay on the old version until we make an upgrade iteration. So I think you should create independent command line versions of importers etc. that don't clash with the Composer implementation.
Great, thanks!
It's not related to composer at all, but it's related to all server side
scripts including that one which is responsible for subject
merging/deletion.
On 30 June 2015 at 13:09, Michael Aufreiter notifications@github.com
wrote:
Just be aware that you don't mix different substance versions. The
Archivist-Composer needs to stay on the old version until we make an
upgrade iteration. So I think you should create independent command line
versions of importers etc. that don't clash with the Composer
implementation.—
Reply to this email directly or view it on GitHub
substance/substance#368 (comment)
.
Hey can you try the following in the archivist repo:
in Package JSON create an entry
{
"substance-new": "substance/substance#master",
}
And then in importers:
var Substance = require("substance-new");
not sure if that works but that would be the best solution i guess.
Good idea! Will try soon.
On 30 June 2015 at 13:18, Michael Aufreiter notifications@github.com
wrote:
not sure if that works but that would be the best solution i guess.
—
Reply to this email directly or view it on GitHub
substance/substance#368 (comment)
.
@michael, no it's not possible. NPM will store substance-new in the same folder inside node_modules
I will use this. It's combination of delete/insert updates. I could use it while document has no annotation. Next challenge would be to use this with already annotated docs somehow...
Hey!
I did it finally!
Look:
var changeTextNode = function(doc, path, startOffset, endOffset, replace) {
var sel = Document.Selection.create(path, startOffset, replace.length);
var range = sel.getRange();
var tx = doc.startTransaction();
tx.update(path, {
delete: {
start: startOffset,
end: endOffset
}
});
Document.AnnotationUpdates.deletedText(tx, path, startOffset, endOffset);
tx.update(path, {
insert: {
offset: startOffset,
value: replace
}
});
Document.AnnotationUpdates.insertedText(tx, range.start, replace.length);
tx.save();
tx.cleanup();
}
I most say... You really need some documentation for core library. It's very hard to trace what you need to do.
Yes. That's true. will come soon.