Hi Dan,

Maybe I explained not correctly: I'm trying to insert text directly in
<caption/>

> Try this: document.insertText(range.getFocus(), "Some text")

Tried to use this, but "Some text" has been inserted before image
doodad, not in <caption/>
Then I tried to get location from ContentElement:

            final ContentElement caption =
document.createChildElement(image, "caption", new HashMap<String,
String>());
            final int location = document.getLocation(caption);
            document.insertText(location, "Some text");

Last line produces exception:

editor (1309769162.559): Invalid LOCAL operation: {__8; ++"Some text";
} Violation: schema violation: element type image does not allow text
content at original document position 8 / resulting document position
8Exception: org.waveprotocol.wave.model.operation.OperationException:
Validation failure: ViolationCollector[1: schema violation: element
type image does not allow text content at original document position 8
/ resulting document position 8] IndexedDoc:
IndexedDI@1feb824[<body><line/>aaaa<image
attaaaaaaa="/piaaaaaaaaaaaa"><caption/></image><line/><image
attaaaaaaa="/piaaaaaaaaaaaa"><caption>aaaa</caption></image><line/>aaaa</body>]

EditorDocFormatter.formatPersistentDomString(...) xml looks like:
<body><line/>text<line/><image
attachment="/pics/hills.jpg"><caption>text</caption></image><line/>text</body>

> (getFocus() is fine, why do you want Math.min?

I looked to the comments of methods:

  /**
   * @return anchor location, may or may not be before the focus
   */
  public int getAnchor() {
    return anchor;
  }

  /**
   * @return focus location, may or may not be before the anchor
   */
  public int getFocus() {
    return focus;
  }

and decided that I need min value from the focus and the anchor.
I'm replaced Math.min with a .getFocus for now:
final Point<ContentNode> point = document.locate(range.getFocus());


2011/7/4 Daniel Danilatos <[email protected]>:
> To insert text, use insertText(location, text).
> "location" can either be an int or a Point. Try to use integer
> locations as much as possible, "Point"s can be tricky and bug prone.
>
> ALL document mutations must be done through the document interface,
> because that takes care of handling sending operations out to the
> server so other clients see them - you cannot modify the document by
> directly manipulating the nodes. See methods on MutableDocument.
>
> Try this: document.insertText(range.getFocus(), "Some text")
> (getFocus() is fine, why do you want Math.min? If you really want to,
> you could also use a Range and call getFirst() instead of using a
> FocusedRange, e.g. SelectionHelper.getOrderedSelectionRange()).
>
> Dan
>
> Στις 4 Ιουλίου 2011 5:45 μ.μ., ο χρήστης Anton Starcev
> <[email protected]> έγραψε:
>> Pat, thanks for responding
>>
>> I inserted image doodad with xml like:
>>
>>      <image attachment="/pics/hills.jpg">
>>        <caption/>
>>      </image>
>>
>> with code:
>>
>>            final CMutableDocument document = editor.getDocument();
>>            final Point<ContentNode> point =
>> document.locate(Math.min(range.getAnchor(), range.getFocus()));
>>            final HashMap<String, String> attributes = new
>> HashMap<String, String>();
>>            attributes.put("attachment", "/pics/hills.jpg");
>>            final ContentElement image = document.createElement(point,
>> "image", attributes);
>>            final ContentElement caption =
>> document.createChildElement(image, "caption", new HashMap<String,
>> String>());
>>
>> As I understand, others doodads work the same way.
>>
>> Now I'm trying to figure out how to insert a text node. That code
>> makes no result:
>>
>> caption.getContainerNodelet().getFirstChild().setNodeValue("image alt text");
>>
>>
>> 2011/7/4 Patrick Coleman <[email protected]>:
>>> I'm not sure of your exact usage, but .locate(int) is part of
>>> LocationMapper, and goes with .getLocation(point)
>>> essentially, the editor's document is both a DOM document (with
>>> ContentNodes, -Elements and -TextNodes), indexable by 'Points' (= defined by
>>> parent + child after)
>>> but also an OT-aware document indexable by integers (e.g. 'insert the text
>>> "hi" at position 4').
>>>
>>> Location mapping helps convert between the two one when required - e.g. in
>>> this case, the selection range is integer offsets (one for anchor, one for
>>> node)
>>> and here it's picking the first of the two (Math.min) then converting that
>>> integer location to a point.
>>> So they are two ways of describing the exact same thing - there are a few
>>> things for which points are better (e.g. inserting an element into the tree)
>>> and a few for which integers are (e.g. finding the first point, OT
>>> operations, ...) so when conversion is required, that's then you use the
>>> Location Mapper.
>>>
>>> hope that helps!
>>> - Pat
>>>
>>>
>>> On 1 July 2011 18:17, Anton Starcev <[email protected]> wrote:
>>>
>>>> Hi,
>>>>
>>>> Trying to insert doodad in editor, found "locate" method:
>>>>
>>>> final Point<ContentNode> point =
>>>> editor.getDocument().locate(Math.min(range.getAnchor(),
>>>> range.getFocus()));
>>>>
>>>> but can't understand how to use it in source code and in this tutorial:
>>>> http://www.waveprotocol.org/code/tutorials/writing-a-doodad
>>>>
>>>>
>>>> Anton Startsev
>>>> ______________________________________________________________________
>>>> [email protected] | www.artlebedev.com
>>>>
>>>
>>
>>
>>
>> --
>> Anton Startsev
>> ______________________________________________________________________
>> [email protected] | www.artlebedev.com
>>
>



-- 
Anton Startsev
______________________________________________________________________
[email protected] | www.artlebedev.com

Reply via email to