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

Editor: linking to a file overwrites selection #15033

Closed
lsinger opened this issue Jun 13, 2017 · 16 comments
Closed

Editor: linking to a file overwrites selection #15033

lsinger opened this issue Jun 13, 2017 · 16 comments
Labels
[Feature] Post/Page Editor The editor for editing posts and pages. Good First Issue Small, contained issues. [Pri] High [Type] Bug

Comments

@lsinger
Copy link
Contributor

lsinger commented Jun 13, 2017

Steps to reproduce

  1. Edit a post or page
  2. Select some text that you want to link to something
  3. Press the (+) button to insert / link to a file
  4. Select a file to link to
  5. Choose "insert"

What I expected

I expected my selected text to be preserved, linking to the file I just selected.

What happened instead

Instead, the selected text was removed and replaced with the filename of the file I inserted.

Screenshot / Video

linking-to-file-overwrites-selection

@lsinger lsinger added [Feature] Post/Page Editor The editor for editing posts and pages. [Type] Bug Good First Issue Small, contained issues. labels Jun 13, 2017
dannylagrouw added a commit to dannylagrouw/wp-calypso that referenced this issue Jun 26, 2017
@adnanhb
Copy link

adnanhb commented Aug 31, 2017

Is anybody working on this issue? If not, I would like to claim it.

@unDemian unDemian self-assigned this Sep 14, 2017
@unDemian
Copy link
Contributor

unDemian commented Sep 15, 2017

I'm not sure if persisting the text should be expected behavior here, because the file selector allows the selection of multiple files, which makes it difficult to have a single word pointing at multiple file's url.

@lsinger
Copy link
Contributor Author

lsinger commented Sep 15, 2017

difficult to have a single word pointing at multiple file's url

Good point. What if we only treat the one-file-case in this special manner?

@unDemian
Copy link
Contributor

unDemian commented Sep 15, 2017

I guess we could treat it as special case, but that might require quite a big change in the code. Because right now all assets from the + Insert Content are appended to the content instead of updating it.

If we are going to keep selected text while inserting new media content, for consistency's sake we should do it for all media types not just files. And if that is the case we should figure out how it behaves in scenarios like:

  • Inserting multiple items while having selected text
  • Inserting images while having selected text
  • Should this also affect forms and google content ?
  • What happens to the selected text when we insert shortcodes for audio and video?

@lsinger
Copy link
Contributor Author

lsinger commented Sep 16, 2017

I still think that there's a special case here.

Pseudocode:

if "text selected and inserting a single non-embedabble item (that will be linked to instead of shown inline)"
    use selected text as link text
else
    remove selected text and insert items (as it is now)

If the effort is really high, though, it might make sense to not invest that right now and wait till Gutenberg has landed. Thoughts?

@unDemian
Copy link
Contributor

That sounds good but from my brief research seems like quite an update and my Calypso class is almost finished so I'll definitely not be able to properly estimate or do it.

@lsinger
Copy link
Contributor Author

lsinger commented Sep 16, 2017

Ah. I wasn't aware that you wanted to do this as part of a class.

@lsinger
Copy link
Contributor Author

lsinger commented Sep 16, 2017

@adnanhb sorry for not replying earlier. If a ticket is unassigned I think you can just start working on it. Maybe add a comment so people know you are. Or self-assign.

@jdlrobson
Copy link
Contributor

^ No tests (can add some if you think solution works), but I think this is what @lsinger is requesting...?

I'm not sure it's worth the effort, given this may produce inconsistent behaviour elsewhere in similar workflows. Also it's not clear what should happen if text is highlighted and two images are selected. Currently the text gets replaced with the two images in that case.

Given @unDemian wanting to rewrite this and the above is this really a "good first change" ?

@lsinger
Copy link
Contributor Author

lsinger commented Jan 17, 2018

Thanks @jdlrobson, this works well for me.

not sure it's worth the effort

I'm happy to go with a solution for just this special case for now, or with forgoing this entirely — especially since Gutenberg is on the horizon.

really a "good first change"

I'm not sure about the specific heuristics here. Given that this issue will be closed soonish either because you supply a fix or because we close it as a wontfix — what would removing the label change, though?

@jdlrobson
Copy link
Contributor

@lsinger is there any way to add tests for tinymce plugins?

@lsinger
Copy link
Contributor Author

lsinger commented Jan 18, 2018

@jdlrobson none that I'm aware of off the bat (which doesn't mean they don't exist).

@lsinger
Copy link
Contributor Author

lsinger commented Jan 18, 2018

@jdlrobson I looked around a bit and found a few TinyMCE plugins in the wp-calypso repository that come with tests:

Do these examples help?

@jdlrobson
Copy link
Contributor

Hi @lsinger, thanks for those examples. Sadly don't quite help me as much as I was hoping.
The functions I want to test are not accessible and the mediaButton is quite large (probably could do with a bit of refactoring), so I need to pull the bits I want to test out into submodules (I don't have time right now to test the whole thing).

Ideally, I would pull out the entire onInsertMedia method and export it as a function that can be bound to the editor... but that's a lot of work given it has lots of dependencies. The easiest thing to do is just pull out and test insertMedia and insertMediaAsLink but that doesn't provide coverage for the if/else switch (which I don't think is vital), on the other hand it does allow us to guard against certain attacks. I've submitted a new patch to hopefully demonstrate this better...

Alternative approach (not covered in current patch) would be something like this:

import getInsertMediaCommand from './commands'
...
<EditorMediaModal {...props} onInsertMedia={getInsertMediaCommand(editor)}
...

commands.js would look something like this:

export function ( editor, renderModal ) {
		return  ( markup, media ) => {
					const selectedText = editor.selection.getContent( { format: 'text' } );
					if ( media.length === 1 && selectedText ) {
						insertMediaAsLink( editor, media[ 0 ] );
					} else {
						insertMedia( editor, markup );
					}
					renderModal( { visible: false } );
				} };
		};
};

I don't really like the feel of doing all that given the rewrite happening.. as @unDemian eluded to it would need some big changes to the code..

@lsinger
Copy link
Contributor Author

lsinger commented Jan 19, 2018

Indeed, that sounds like more effort than would currently be warranted.

Given that a) the rewrite is happening and b) many other TinyMCE plugins in the Calypso codebase also don't have any tests at all — I wouldn't be too concerned if this small change shipped without a test ... unless some sort of attack vector could be argued for, which I don't think is the case here — malicious code could wreck the attacker's own editor instance, but I don't currently see any way for this to affect other users.

@apeatling apeatling added this to the Product Quality milestone Jan 31, 2018
@apeatling apeatling modified the milestones: Product Quality, Product Quality: Editor Flows Jan 31, 2018
jdlrobson added a commit to jdlrobson/wp-calypso that referenced this issue Mar 8, 2018
)

If some text has been selected and only one media item is selected
exec the replaceContent command in tinymce.

Changes:
* The onInsertMedia function callback now takes a second parameter
- the raw list of media. This allows other uses to simply embedding the
media item in the article post.
@rachelmcr
Copy link
Member

I confirmed this issue still exists in Calypso, and tested it in Gutenberg.

Gutenberg doesn't yet support this kind of action (you have to know the file URL to link to it, unless using a separate File block) but it's being explored in WordPress/gutenberg#8322. I'm going to close this issue since the Calypso fix is more complex than expected and the issue doesn't exist in the same way in Gutenberg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Post/Page Editor The editor for editing posts and pages. Good First Issue Small, contained issues. [Pri] High [Type] Bug
Projects
None yet
Development

No branches or pull requests

6 participants