Text

Answered

add block failed: could not create block at (x,y)

I am building an app with SwiftUI for iPad and I get an error when creating a "Text Document" and try to perform addBlock with some text I want to import to this view.

Error while creating package : The operation couldn’t be completed. Error, -[IINKEditor addBlock:type:mimeType:data:error:]::428 add block failed: could not create block at (x,y)

My code for adding a block:

let myText = NSAttributedString(string: """

Test inputing text.


Test input 2.

""")

try editorViewController.editor.addBlock((editorViewController.editor.rootBlock?.box.origin)!, type: "Text", mimeType: .text, data: myText.string  ) 

I have also tried 

try editorViewController.editor.addBlock((CGPoint(x: 0, y: 0), type: "Text", mimeType: .text, data: myText.string  ) 

and


try editorViewController.editor.addBlock((.zero, type: "Text", mimeType: .text, data: myText.string  ) 


All of these result in a failure to add a block.  When I do this process as a "Text" block using import it works.

 


Best Answer

Dear James,

Thank you for contacting us.

I recommend you refer to the editing->block-addtion part of the documentation: https://developer.myscript.com/docs/interactive-ink/1.2/android/fundamentals/editing/#block-addition

You can also check the API documentation: https://developer.myscript.com/refguides/interactive-ink/android/1.3/com/myscript/iink/Editor.html#addBlock-float-float-java.lang.String-

From this, you will see the addBlcok functions can only be used in a "Text Document" part, not a "Text" part.

The AddBlock function doesn't allow to set the height and width of a block, all you provide is the "approximate" position of where you want the top-left corner of your block to be, and the iink will try to put the block where it can. If there is not enough place, it will not add the block and will raise an IllegalStateException exception.

Then, when adding data in your block, it will be re-sized accordingly, if it can: https://developer-support.myscript.com/en/support/discussions/topics/16000024723

Best regards,

Olivier


I am creating a Text Document and still get the same error.  The below function basically copied from the swift example code.


func createPackage(packageName: String) throws -> IINKContentPackage? {

 // Create a new content package with name

        var resultPackage: IINKContentPackage?

        let fullPath = FileManager.default.pathForFile(inDocumentDirectory: packageName) + ".iink"

        if let engine = (UIApplication.shared.delegate as? AppDelegate)?.engine {

            resultPackage = try engine.createPackage(fullPath.decomposedStringWithCanonicalMapping)

 

 // Add a blank page type Text Document

 if let part = try resultPackage?.createPart("Text Document") /* Options are : "Diagram", "Drawing", "Math", "Text Document", "Text" */ {

                self.title = "Type: " + part.type

 }

 }

        return resultPackage

 }


After creating the package I get the part then I try to add the block.

try editorViewController.editor.part = package.getPartAt(0)

try editorViewController.editor.addBlock(.zero, type: "Text Document", mimeType: .text, data: string)


Still same error

Error, -[IINKEditor addBlock:type:mimeType:data:error:]::428 cannot add a block of type Text Document

Error while creating package : The operation couldn’t be completed. Error, -[IINKEditor addBlock:type:mimeType:data:error:]::428 cannot add a block of type Text Document


Dear James,


thank you for the update.


The reason of the error is that you are trying to add a "Text Document" block inside your "Text Document" part, which is not possible.


In a "Text Document" part, you can add Math, Text, or Diagram blocks, but you cannot add a "Text Document" (please note a Text Document is a container and cannot be a block).


In your case, you can't add a "Text" block instead?


Best regards,


Olivier

The only thing I changed was in the addBlock I changed it to Text Document thinking that's what you were suggesting. 

What I am doing is importing an initial text block. I am not trying to add it to the existing text.


So when I instantiate this view I am creating a "Text Document" then trying to add some initial text.

Dear James,


basically, all you need to ensure is that there is "enough place" to add the block. In a "blank" "Text Document", just try to add a block at (0,0) position, you should be able to add it.


Also, if you are trying to add a block in an existing block, or "outside of your document" it will fail too.


If you have a video that show where you try to add the document, it will likely confirm the above hypothesis.


Best regards,


Olivier

Sorry, it took me so long. I broke my project so I had to redo a lot of work. You can find my project here. git@gitlab.com:james154/swifthandwriting.git


The file where I have the problem is in swifthandwriting/SwiftHandwritting/MyHandwritingView.swift.

https://gitlab.com/james154/swifthandwriting/-/blob/master/SwiftHandwritting/MyHandwritingView.swift#L289

Here I have created a Text block and this works. However, when I change it to "Text Document"

I cannot successfully perform addBlock with text.


https://gitlab.com/james154/swifthandwriting/-/blob/master/SwiftHandwritting/MyHandwritingView.swift#L262

Here I am importing text and this is successful. However, if I change line 289 to Text Document then addBlock instead of import_ and it fails to add the text.


It's been a long time but when I first did the Swift example I did addBlock and it worked.

I will post the code when I can. What you have said is what I did. In fact I have done this with the example project. Is it possible to add an empty text block the import text into that?

Dear James,


I think we will need your projec to reproduce ; but I am insisting, when teh addBlock fails, it is usually one of the above cases I mentionned. Please let us know exactly the way you add the block.


Best regards,


Olivier

Dear James,


currently, I played with your sample, and came to the following conclusions:

-Either you choose to use a Text part, or a "Text Document".

-If using a "Text" part, you will then be able to import Text ; but the latter will not allow you to use the addBlock function

-If using the "Text Document" part, you cannot import Text. If you want to add text, the easier is that you use the addBlock function. Nevertheless, it seems you are not using the addBlock function properly. The easier is that you proceed as in the following topic: https://developer-support.myscript.com/support/discussions/topics/16000027123


Best regards,


Olivier

I looked at the example you gave me and add the below code based on the link you just gave me. The result if "-[IINKEditor addBlock:type:error:]::416 invalid text data (cannot create text block with empty data)"

If I do the statement I did before where I give the text I want to input it gives the same error as before. When I have time I will go back to your example swift code and dod the same thing and see if it actually works.

 

try editorCtrl.editor.part = package.getPartAt(0)

let p = CGPoint(x: 0, y: 5)
let r = editorCtrl.editor.renderer
let v = r.viewTransform
p.applying(v)
try editorCtrl.editor.addBlock(p, type: "Text")

 

The difference between the example you pointed me to and what I want to do is that I want to import some initial text. I don’t want to add text after some existing block. Is there some way to define an independent text block and then save that to the root block?
Went back and tried this on the example GetStartedSwift addBlock after creating the part, just like in my code, will actually work. So I copied the storyboard from the example and the HomeViewContriller.swift file to my project and used a UIViewControllerRepresentable to connect it to the swiftui code. Got it working in only a few minutes. When I clean it up I will post it.
Answer

Dear James,

Thank you for contacting us.

I recommend you refer to the editing->block-addtion part of the documentation: https://developer.myscript.com/docs/interactive-ink/1.2/android/fundamentals/editing/#block-addition

You can also check the API documentation: https://developer.myscript.com/refguides/interactive-ink/android/1.3/com/myscript/iink/Editor.html#addBlock-float-float-java.lang.String-

From this, you will see the addBlcok functions can only be used in a "Text Document" part, not a "Text" part.

The AddBlock function doesn't allow to set the height and width of a block, all you provide is the "approximate" position of where you want the top-left corner of your block to be, and the iink will try to put the block where it can. If there is not enough place, it will not add the block and will raise an IllegalStateException exception.

Then, when adding data in your block, it will be re-sized accordingly, if it can: https://developer-support.myscript.com/en/support/discussions/topics/16000024723

Best regards,

Olivier