dankito / RichTextEditor

Rich text WYSIWYG editor for Android and JavaFX
Apache License 2.0
92 stars 36 forks source link

Upload inserted images #5

Closed E-Mans-Application closed 6 years ago

E-Mans-Application commented 6 years ago

Hello,

That's me again. I'm trying to implement an automatic image upload when they are inserted into the editor. I use:

@Override
protected void onCreate(final Bundle savedInstanceState) {
[...]
    editorToolbar.addCommandInvokedListener(this::editorCommandInvoked);
[...]
}
private Unit editorCommandInvoked(final ToolbarCommand command){

        switch(command.getCommand()){

            case INSERTIMAGE:
                //TODO UPLOAD IMAGE
                break;

        }

        return Unit.INSTANCE;

}

I don't know how to:

Is it currently possible?

dankito commented 6 years ago

The CommandInvoked listener gets fired if a toolbar button is pressed.

It's simply there so that the toolbar commands know that they now have to do their work.

It does not say anything if the HTML has changed or not.

Actually there's currently no mechanism telling you 'an image has been inserted'.

It would take me some effort to implement it, but anyway for your synchronizing issue I actually think it's the wrong approach. Imagine the following cases:

I'd rather wait till the document gets saved and then use the mechanism posted somewhere else to determine which images have been added and which ones have been removed.

For keeping track of the images you could do the following:

So it does not matter if the user currently has internet access or not, as soon as it's back again the upload starts.

You may also can add a counter, how often that image is used. Imagine an image is added two times in different documents -> counter is 2. Then it gets removed from one document -> counter decreases to 1, but as it's still larger than 0 you know that you don't have to remove it from server yet.

By the way, there comes to my mind, that local images currently get added with their absolute path to document. In order for synchronization to properly work I have to added them with relative path.

E-Mans-Application commented 6 years ago

You're right.

I used your suggestion at #2 (by replacing href with src, href is for links)

The upload was already in background and didn't prevent the user from continuing editing, but I now make all uploads when the user saves the document. If the uploads fails, the local url is kept, and the application will attempt a new upload on next save.

And do not worry, the server already checks whether a image is still used before deleting it.

~I should still implement a hash-check to prevent the app from re-uploading images that are already uploaded.~ Hash-check implemented.