Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text manipulation API to be used with macros #368

Open
michael opened this issue Jun 22, 2015 · 16 comments
Open

Text manipulation API to be used with macros #368

michael opened this issue Jun 22, 2015 · 16 comments
Assignees

Comments

@michael
Copy link
Member

michael commented Jun 22, 2015

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?

@Integral
Copy link
Member

@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.

@obuchtala
Copy link
Member

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)

@obuchtala
Copy link
Member

... 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.

@obuchtala
Copy link
Member

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.

@obuchtala
Copy link
Member

Annotation updates are done using https://github.com/substance/substance/blob/master/src/document/annotation_updates.js
(is also there in your version)

@Integral
Copy link
Member

@oliver----, thanks!
Is it possible to use new api only on server side?

@obuchtala
Copy link
Member

Yes, should be possible on server side.

@michael
Copy link
Member Author

michael commented Jun 30, 2015

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.

@Integral
Copy link
Member

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 [email protected]
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)
.

@michael
Copy link
Member Author

michael commented Jun 30, 2015

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");

@michael
Copy link
Member Author

michael commented Jun 30, 2015

not sure if that works but that would be the best solution i guess.

@Integral
Copy link
Member

Good idea! Will try soon.

On 30 June 2015 at 13:18, Michael Aufreiter [email protected]
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)
.

@Integral
Copy link
Member

Integral commented Jul 1, 2015

@michael, no it's not possible. NPM will store substance-new in the same folder inside node_modules

@Integral
Copy link
Member

Integral commented Jul 1, 2015

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...

@Integral
Copy link
Member

Integral commented Jul 3, 2015

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.

@obuchtala
Copy link
Member

Yes. That's true. will come soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants