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

Codeblock copy button #3501

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from
Open

Conversation

sxyxs
Copy link

@sxyxs sxyxs commented May 24, 2022

Description

Added a plugin to add a button to copy the contents of a codeblock as #2375.

Changes

  1. Added the plugin source/common/modules/markdown-editor/plugins/render-codeblock-button.ts
  2. Modified relevant files to enable toggling the plugin in display preferences.
  3. New translation string: dialog.preferences.display.render_codeblock_button

Additional Information

  • error when running yarn lint:
    • in file source/common/modules/markdown-editor/plugins/render-codeblock-button.ts
    • error The array literal notation [] is preferable
    • However, if we define the array with [] on line 29 , I cannot do the clipboard.writeText operation on line 64.

Tested on:
Ubuntu 20.04 LTS

@boring-cyborg
Copy link

boring-cyborg bot commented May 24, 2022

Thank you for opening your first PR! 🎉 We are very happy and would like to thank you very much for your contribution. If everything checks out, we'll make sure to review the PR as soon as possible and give feedback. In the meantime, to make the reviewing process as fast as possible, you can help us by checking the following things:

  • Did you follow the JSStandard coding style? - Did you comment everywhere where the necessity of a piece of code or the
    way it was implemented is not immediately obvious?
  • Did you attempt to stick as much to current coding habits as possible?
    (Note that this does not apply to pieces of code where we ourselves
    obviously violated good coding practices, which unfortunately happens
    sometimes. But please indicate this in your PR so that we know what you
    rectified!)

Furthermore, make sure that the linter does not complain, which will check your code on every new commit. If the linter task fails, make sure to run yarn lint locally and check the file eslint_report.htm which will tell you precisely what went wrong.
Stay sharp, and thanks again!

@EdgarTang
Copy link
Contributor

EdgarTang commented May 24, 2022

  • error when running yarn lint:

    • in file source/common/modules/markdown-editor/plugins/render-codeblock-button.ts
    • error The array literal notation [] is preferable
    • However, if we define the array with [] on line 29 , I cannot do the clipboard.writeText operation on line 64.

In typescript you can declare a sting array like:
let codesblocks = new Array<string>()
More examples here.
By the way, you should better remove the test code like console.log(i) in source/common/modules/markdown-editor/plugins/render-codeblock-button.ts line 48

@Mic112345
Copy link

  • error when running yarn lint:

    • in file source/common/modules/markdown-editor/plugins/render-codeblock-button.ts
    • error The array literal notation [] is preferable
    • However, if we define the array with [] on line 29 , I cannot do the clipboard.writeText operation on line 64.

In typescript you can declare a sting array like: let codesblocks = new Array<string>() More examples here. By the way, you should better remove the test code like console.log(i) in source/common/modules/markdown-editor/plugins/render-codeblock-button.ts line 48

Thanks for the feedback, I've addressed those areas in the most recent commit

@sxyxs sxyxs marked this pull request as ready for review May 27, 2022 05:18
@sxyxs
Copy link
Author

sxyxs commented May 28, 2022

Hi @nathanlesage, could you help us to review this pull request?

@sxyxs
Copy link
Author

sxyxs commented May 28, 2022

Hi @kyaso, could you help us to review this pull request?

Copy link
Collaborator

@kyaso kyaso left a comment

Choose a reason for hiding this comment

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

Overall, this looks promising! I have added some comments. Maybe @nathanlesage can also have a look when he has time again.

One thing I noticed: When you edit an existing codeblock, save the document, and then click the copy button, the newly added lines are not copied; only the previous version of the codeblock is copied. Not sure what might be causing this, but it's something we probably want to address before merging.

source/win-preferences/schema/display.ts Outdated Show resolved Hide resolved
@@ -210,6 +210,22 @@ body {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}

.code-block-copy-button{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to make the button and its placement look similar to this: #2375 (comment)

Currently, the button looks a bit out of place:
grafik

@sxyxs
Copy link
Author

sxyxs commented May 28, 2022

Overall, this looks promising! I have added some comments. Maybe @nathanlesage can also have a look when he has time again.

One thing I noticed: When you edit an existing codeblock, save the document, and then click the copy button, the newly added lines are not copied; only the previous version of the codeblock is copied. Not sure what might be causing this, but it's something we probably want to address before merging.

Thank you for the reply. We will fix that.

@nathanlesage
Copy link
Member

Heya, I'm back from a week-long conference, and just had a look. First of all, thank you for your efforts, it looks as if this will be a great addition to the app! Also, thanks to @kyaso for providing a very good and extensive code review. I think his review provides very good guidance in improving your PR. I added a few comments in there where I deemed it fit, but please orient yourself at Kyaso's comments first for a first round of review! :)

@sxyxs
Copy link
Author

sxyxs commented Jun 4, 2022

Hi, I update my code based on the feedback from @kyaso. Also, I do not really understand the meaning of "Do not alter the language files." from @nathanlesage feedback. Does it mean I need to remove that line?

@nathanlesage
Copy link
Member

@sxyxs This meant that you should not modify any of the language files. We have to do it via Zettlr Translate, i.e.: I need you to provide a list of new identifiers that you've introduced in your PR, and then I can add them to Zettlr Translate for everyone to translate.

@sxyxs
Copy link
Author

sxyxs commented Jun 6, 2022

@sxyxs This meant that you should not modify any of the language files. We have to do it via Zettlr Translate, i.e.: I need you to provide a list of new identifiers that you've introduced in your PR, and then I can add them to Zettlr Translate for everyone to translate.

I understand that and start dealing with it. Thank you for the reply.

@sxyxs
Copy link
Author

sxyxs commented Jun 9, 2022

Hi @nathanlesage, the only thing we have changed is to add a new copy button in each code block introduced in the PR's introduction.
Screenshot from 2022-06-09 21-03-49

@sxyxs sxyxs requested a review from kyaso June 14, 2022 11:10
@sxyxs
Copy link
Author

sxyxs commented Jun 20, 2022

@kyaso Thanks for your feedback and sorry for missing one point. I have fixed that.

source/common/less/theme-berlin/light.less Outdated Show resolved Hide resolved
source/common/less/theme-berlin/light.less Show resolved Hide resolved
source/common/less/theme-berlin/light.less Outdated Show resolved Hide resolved
source/common/less/theme-berlin/light.less Outdated Show resolved Hide resolved
source/common/less/theme-berlin/light.less Outdated Show resolved Hide resolved
@sxyxs
Copy link
Author

sxyxs commented Jun 23, 2022

Hi @kyaso, I have already fixed what you want me to fix. Please check.

@kyaso
Copy link
Collaborator

kyaso commented Jun 23, 2022

@sxyxs It looks fine to me. I think there is one minor improvement left which you could implement: #3501 (comment)

@nathanlesage Would you mind having a final look over it again? Also, currently, the button text is a hardcoded "Copy", but I guess it should be translated? And last point: Do you think we should squash some commits before merging to clean up the commit history a bit?

@sxyxs
Copy link
Author

sxyxs commented Jun 24, 2022

@sxyxs It looks fine to me. I think there is one minor improvement left which you could implement: #3501 (comment)

@nathanlesage Would you mind having a final look over it again? Also, currently, the button text is a hardcoded "Copy", but I guess it should be translated? And last point: Do you think we should squash some commits before merging to clean up the commit history a bit?

Hi @kyaso, fixed, please check.

@sxyxs sxyxs requested a review from kyaso July 4, 2022 11:03
@kyaso
Copy link
Collaborator

kyaso commented Jul 7, 2022

@sxyxs Looks fine from my side.

Would you mind rebasing your commits on top of the latest develop branch and also squash the temporary commits to clean up the commit history? And once that's done, @nathanlesage could have another look, when he has time?

@sxyxs
Copy link
Author

sxyxs commented Jul 10, 2022

@sxyxs Looks fine from my side.

Would you mind rebasing your commits on top of the latest develop branch and also squash the temporary commits to clean up the commit history? And once that's done, @nathanlesage could have another look, when he has time?

Ok, I will do it these days.

@sxyxs
Copy link
Author

sxyxs commented Jul 14, 2022

Hi @kyaso, Is that what you want?

@sxyxs
Copy link
Author

sxyxs commented Jul 26, 2022

@kyaso Hello?

@nathanlesage
Copy link
Member

@sxyxs I think Kyaso is on vacation at the moment. I will have a look at the PR in the upcoming days.

@sxyxs
Copy link
Author

sxyxs commented Aug 1, 2022

@sxyxs I think Kyaso is on vacation at the moment. I will have a look at the PR in the upcoming days.

That will be great, thank you!

Copy link
Member

@nathanlesage nathanlesage left a comment

Choose a reason for hiding this comment

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

I found a few things, styling issues mainly. Please fix them while I pull the thing and test it out locally.

const lineCount = cm.lineCount()
let incodeblock = false
let codesblocks = new Array<string>()
let codeblock = ''
Copy link
Member

Choose a reason for hiding this comment

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

Why don't you store the codeblock as an array? That would (a) make line 39 less awkward since you just had to push the line, would (b) save you from slicing in line 44, and (c) in line 71 you could simply write codeblock.join('\n') into the clipboard.

const codeBlockRE = /^(?:\s{0,3}`{3}|~{3}).*/
const lineCount = cm.lineCount()
let incodeblock = false
let codesblocks = new Array<string>()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let codesblocks = new Array<string>()
const codeblocks: string[] = []

if (document.getElementsByClassName(codeblockClassClose).length === codeblocks.length) {
for (let i = 0; i < codeblocks.length; i++) {
const codeBlock = codeblocks[i]
if (codeBlock === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

This should never be true! Did you add this because you were encountering undefined-issues? If so then there is a serious bug further above in this file. If not, then it should be safe to delete this.

Copy link
Author

Choose a reason for hiding this comment

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

@nathanlesage Yes, there will have some issues if I delete this one. I will take look into it to find a better solution.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @nathanlesage, should I delete line 53 or 56 or both?

codeBlock.childNodes[1].addEventListener('click', () => clipboard.writeText(codeBlockText))
} else {
// Add property for copy button
let copyButton = document.createElement('button')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let copyButton = document.createElement('button')
const copyButton = document.createElement('button')

let copyButton = document.createElement('button')
copyButton.className = 'code-block-copy-button'
copyButton.innerHTML = '<clr-icon shape="copy"></clr-icon>'
copyButton.setAttribute('title', 'Copy this code block to clipboard')
Copy link
Member

Choose a reason for hiding this comment

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

Please make this translatable and provide the translation keys for me to add to Zettlr translate once it's finished.

@@ -62,6 +62,11 @@ export default function (): any {
type: 'checkbox',
label: trans('dialog.preferences.display.render_emphasis'),
model: 'display.renderEmphasis'
},
{
type: 'checkbox',
Copy link
Member

Choose a reason for hiding this comment

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

I think it is safe to not make this toggleable. The codeblock button does not change the rendering of any part of the editor, hence we should remove this option. Possibly this also involves removing the option from other files your PR touches.

@@ -182,7 +182,8 @@ export default defineComponent({
math: (global as any).config.get('display.renderMath'),
tasks: (global as any).config.get('display.renderTasks'),
headingTags: (global as any).config.get('display.renderHTags'),
tables: (global as any).config.get('editor.enableTableHelper')
tables: (global as any).config.get('editor.enableTableHelper'),
codeBlockButton: (global as any).config.get('display.renderCodeBlockButton')
Copy link
Member

Choose a reason for hiding this comment

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

The quicklook editor has been nuked in the meantime, so you can remove this change.

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.

None yet

7 participants