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

Add architecture for granual pasteAsPlainText support #68

Merged
merged 6 commits into from
Sep 21, 2022

Conversation

anleac
Copy link

@anleac anleac commented Sep 15, 2022

Motive

We wish to be able to have granual control over specifically the behaviour of whether to linkify when pasting a link over selected text. This pull request extends the current code base to support granual config support on the installation of the package, at this time, it only adds one control being for the paste-markdown-link (urlLinks in our API params).

This config, which is default set to false (undefined | false), is the existing behaviour and therefore not a breaking change.

The approach would therefore add the following "new" behaviour, given the pasteLinkAsPlainTextOverSelectedText flag for paste-markdown-link, and depending on whether the skipFormattingFlag is on/off:

  • paste plain text is on, no skip flag -> paste as plain
  • paste plain text is on, skip flag -> try linkify
  • paste plain text is off, no skip flag -> try linkify
  • paste plain text is off, skip flag -> paste as plain

As we add future support, we should extend this down to the future classes + add tests, I do not believe it would be much extra work but let's do it as a follow up or as a per-need basis.

Drawbacks

End-users may see this optional config as misleading, as we are passing in the options params to all base paste classes but only have it supported in one currently, but the documentation will reflect this.

@anleac anleac requested a review from a team as a code owner September 15, 2022 18:10
@mboisvertdupras
Copy link

Looks good! 👍

Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some fragility to storing state on DOM nodes. Did you consider a weak map of element to value?

src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
@anleac
Copy link
Author

anleac commented Sep 16, 2022

There's some fragility to storing state on DOM nodes. Did you consider a weak map of element to value?

Indeed, I considered having a weak map inside the paste-markdown-link for element -> boolean mapping. I went back/fourth on this approach just due to having to extend the install functions on each paste-markdown-* file (due to the installation pattern here: https://github.com/github/paste-markdown/blob/main/src/paste-keyboard-shortcut-helper.ts#L27

That being said, I am happy to update to this if you believe this is a stronger approach 👍

@theinterned
Copy link
Contributor

can you explain the difference between shouldPastePlainText and shouldSkipFormatting

@anleac
Copy link
Author

anleac commented Sep 16, 2022

can you explain the difference between shouldPastePlainText and shouldSkipFormatting

The difference is that, even in the event of shouldPastePlainText being true, we would still like the option to "skip formatting" of that value, which would shift it to linkifying.

@anleac
Copy link
Author

anleac commented Sep 16, 2022

There's some fragility to storing state on DOM nodes. Did you consider a weak map of element to value?

I also just pushed a commit here cd58a52 to use a WeakMap instead, and updated the description

@theinterned
Copy link
Contributor

theinterned commented Sep 16, 2022

we would still like the option to "skip formatting" of that value, which would shift it to linkifying.

@anleac – so this would allow for a config that does not apply other formatting like images, tables, bold etc. but DOES linkify still?

Another way to do that is just to subscribe to a subset of the functionality.

- const unsubscribe = subscribe(el)
+ installLink()

@anleac
Copy link
Author

anleac commented Sep 16, 2022

we would still like the option to "skip formatting" of that value, which would shift it to linkifying.

@anleac – so this would allow for a config that does not apply other formatting like images, tables, bold etc. but DOES linkify still?

Yes that was the current motive - it would be similar to the toggle switch Edge has here (it is not the same, but similar) in being able to toggle rich HTML or plain text by default

image

@anleac
Copy link
Author

anleac commented Sep 20, 2022

My proposal, which I updated in 2661306, we can make the config name very specific (pasteLinkAsPlainTextOverSelectedText) to try to mitigate end user confusion, and we can update to add more modular control afterwards if needed.

@anleac anleac changed the title Support config param for shouldPastePlainText Support config param for pasteLinkAsPlainTextOverSelectedText Sep 20, 2022
Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like where you've landed on this. It has room to scale to other formatters if needed. Note.

I do have the one API question – I think it's worth keeping the signature of all install functions parallel.

I would also like to see some docs added to the readme about this.

}

interface PlainTextParams {
urlLinks?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!

Comment on lines 28 to 29
installCallbacks: Array<(el: HTMLElement) => void>,
installCallbacksWithOptions: Array<(el: HTMLElement, optionConfig?: OptionConfig) => void>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than have two arrays here, why not just pass options to all the callbacks?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to avoid confusion given we export the individual installation functions, too, but the documentation update would be sufficient. Let me tackle this now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infact I am not even sure this is needed, I believe still keeping them to the same array and simplying defaulting to passing in config params in all cases works (tests are still passing).

image

If my understanding is correct, it'll simply ignore the param

@anleac
Copy link
Author

anleac commented Sep 20, 2022

I really like where you've landed on this. It has room to scale to other formatters if needed. Note.

I do have the one API question – I think it's worth keeping the signature of all install functions parallel.

I would also like to see some docs added to the readme about this.

👋 my concern with sharing the installation signature across all the functions is that only one actually can be influenced by the config, while the rest wouldn't be, and we export all of these installations which possibly could be misleading. I guess we can circumvent this via the docs and marking this as not yet implemented?

@theinterned
Copy link
Contributor

I guess we can circumvent this via the docs and marking this as not yet implemented?

yeah ... that was my thought. The actual type signature for the Options should hopefully make it clear what the options are.

I guess the other option would be to have the install commands implement either an Install or InstallWithOptions type and then do type narrowing of some kind in installAround.

@anleac anleac changed the title Support config param for pasteLinkAsPlainTextOverSelectedText Add architecture for granual pasteAsPlainText support Sep 21, 2022
Copy link
Contributor

@theinterned theinterned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looks great! Thank you for being patient and working through the multiple rounds of feedback. I think the extra work should pay off in keeping this API healthy.

Only the `urlLinks` param is currently supported.

If there is no config passed in, or attributes missing, this will always default to `false`, being the existing behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you this is great 👏

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

Successfully merging this pull request may close these issues.

4 participants