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

[$250] [TS migration] Migrate 'Clipboard' lib to TypeScript #24887

Closed
melvin-bot bot opened this issue Aug 16, 2023 · 21 comments
Closed

[$250] [TS migration] Migrate 'Clipboard' lib to TypeScript #24887

melvin-bot bot opened this issue Aug 16, 2023 · 21 comments
Assignees
Labels
Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor Reviewing Has a PR in review Task Typescript Migration

Comments

@melvin-bot
Copy link

melvin-bot bot commented Aug 16, 2023

TypeScript migration

Make sure you read through our TypeScript's style guide, cheatsheet and PropTypes conversion table before you start working on this migration issue.

Files

Path Dependencies
src/libs/Clipboard/index.native.js 1
src/libs/Clipboard/index.js 5
Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~01380b48af60123d4a
  • Upwork Job ID: 1717487195847479296
  • Last Price Increase: 2023-11-02
@melvin-bot
Copy link
Author

melvin-bot bot commented Sep 11, 2023

This issue has not been updated in over 15 days. eroding to Monthly issue.

P.S. Is everyone reading this sure this is really a near-term priority? Be brave: if you disagree, go ahead and close it out. If someone disagrees, they'll reopen it, and if they don't: one less thing to do!

@teneeto
Copy link
Contributor

teneeto commented Sep 19, 2023

Hi, I'm Eto from Callstack - expert contributor group - and I would like to take a look at this issue.

@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Monthly KSv2 labels Oct 26, 2023
@Julesssss Julesssss added the External Added to denote the issue can be worked on by a contributor label Oct 26, 2023
@melvin-bot melvin-bot bot changed the title [TS migration] Migrate 'Clipboard' lib to TypeScript [$500] [TS migration] Migrate 'Clipboard' lib to TypeScript Oct 26, 2023
@melvin-bot
Copy link
Author

melvin-bot bot commented Oct 26, 2023

Job added to Upwork: https://www.upwork.com/jobs/~01380b48af60123d4a

@melvin-bot melvin-bot bot added the Help Wanted Apply this label when an issue is open to proposals by contributors label Oct 26, 2023
@melvin-bot
Copy link
Author

melvin-bot bot commented Oct 26, 2023

Triggered auto assignment to Contributor-plus team member for initial proposal review - @sobitneupane (External)

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Oct 26, 2023
@ZhenjaHorbach
Copy link
Contributor

ZhenjaHorbach commented Oct 26, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

Need update clipboard lib using TS

What is the root cause of that problem?

It's part of the migration from JS to TS

What changes do you think we should make in order to solve the problem?

For the native part, minimal changes are required since this library is practically not used on native devices
https://github.com/Expensify/App/blob/main/src/libs/Clipboard/index.native.js

For example

import Clipboard from '@react-native-clipboard/clipboard';

/**
 * Sets a string on the Clipboard object via @react-native-clipboard/clipboard
 */
const setString = (text: string): void => {
    Clipboard.setString(text);
};

export default {
    setString,

    // We don't want to set HTML on native platforms so noop them.
    canSetHtml: (): boolean => false,
    setHtml: (): void => {},
};

For web part we need more changes
https://github.com/Expensify/App/blob/main/src/libs/Clipboard/index.js

For example

import Clipboard from '@react-native-clipboard/clipboard';
import lodashGet from 'lodash/get';
import CONST from '../../CONST';
import * as Browser from '../Browser';

const canSetHtml = (): boolean => lodashGet(navigator, 'clipboard.write');

/**
 * Deprecated method to write the content as HTML to clipboard.
 */
function setHTMLSync(html: string, text: string): void {
    const node = document.createElement('span');
    node.textContent = html;
    node.style.all = 'unset';
    node.style.opacity = '0';
    node.style.position = 'absolute';
    node.style.whiteSpace = 'pre-wrap';
    node.style.userSelect = 'text';
    node.addEventListener('copy', (e) => {
        e.stopPropagation();
        e.preventDefault();
        e.clipboardData.clearData();
        e.clipboardData.setData('text/html', html);
        e.clipboardData.setData('text/plain', text);
    });
    document.body.appendChild(node);

    const selection = window.getSelection();
    const firstAnchorChild = selection.anchorNode && selection.anchorNode.firstChild;
    const isComposer = firstAnchorChild instanceof HTMLTextAreaElement;
    let originalSelection = null;
    if (isComposer) {
        originalSelection = {
            start: firstAnchorChild.selectionStart,
            end: firstAnchorChild.selectionEnd,
            direction: firstAnchorChild.selectionDirection,
        };
    } else {
        originalSelection = {
            anchorNode: selection.anchorNode,
            anchorOffset: selection.anchorOffset,
            focusNode: selection.focusNode,
            focusOffset: selection.focusOffset,
        };
    }

    selection.removeAllRanges();
    const range = document.createRange();
    range.selectNodeContents(node);
    selection.addRange(range);

    try {
        document.execCommand('copy');
    } catch (e) {
        // The 'copy' command can throw a SecurityError exception, we ignore this exception on purpose.
        // See https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#the-copy-command for more details.
    }

    selection.removeAllRanges();

    if (isComposer) {
        firstAnchorChild.setSelectionRange(originalSelection.start, originalSelection.end, originalSelection.direction);
    } else {
        selection.setBaseAndExtent(originalSelection.anchorNode, originalSelection.anchorOffset, originalSelection.focusNode, originalSelection.focusOffset);
    }

    document.body.removeChild(node);
}

/**
 * Writes the content as HTML if the web client supports it.
 */
const setHtml = (html: string, text: string): void => {
    if (!html || !text) {
        return;
    }

    if (!canSetHtml()) {
        throw new Error('clipboard.write is not supported on this platform, thus HTML cannot be copied.');
    }

    if (CONST.BROWSER.SAFARI === Browser.getBrowser()) {
        // Safari sanitizes "text/html" data before writing to the pasteboard when using Clipboard API,
        // whitespaces in the start of the line are stripped away. We use the deprecated method to copy
        // contents as HTML and keep whitespaces at the start of the line on Safari.
        // See https://webkit.org/blog/10855/async-clipboard-api/ for more details.
        setHTMLSync(html, text);
    } else {
        navigator.clipboard.write([
            // eslint-disable-next-line no-undef
            new ClipboardItem({
                'text/html': new Blob([html], { type: 'text/html' }),
                'text/plain': new Blob([text], { type: 'text/plain' }),
            }),
        ]);
    }
};

/**
 * Sets a string on the Clipboard object via react-native-web
 */
const setString = (text: string): void => {
    Clipboard.setString(text);
};

export default {
    setString,
    canSetHtml,
    setHtml,
};

What alternative solutions did you explore? (Optional)

NA

@fabioh8010
Copy link
Contributor

@Julesssss Why External label was added to this issue? We already have a PR for that -> #28789

@Julesssss
Copy link
Contributor

Why External label was added to this issue? We already have a PR for that -> #28789

Just to find a C+ reviewer, and someone to pay the C+ reviewer.

@situchan
Copy link
Contributor

The only remaining here is to remove Help Wanted label.

@Julesssss Julesssss removed the Help Wanted Apply this label when an issue is open to proposals by contributors label Oct 26, 2023
Copy link
Author

melvin-bot bot commented Nov 2, 2023

@sobitneupane Whoops! This issue is 2 days overdue. Let's get this updated quick!

@Christinadobrzyn
Copy link
Contributor

@Christinadobrzyn Christinadobrzyn changed the title [$500] [TS migration] Migrate 'Clipboard' lib to TypeScript [$250] [TS migration] Migrate 'Clipboard' lib to TypeScript Nov 2, 2023
Copy link
Author

melvin-bot bot commented Nov 2, 2023

Upwork job price has been updated to $250

Copy link
Author

melvin-bot bot commented Nov 6, 2023

@sobitneupane 6 days overdue. This is scarier than being forced to listen to Vogon poetry!

Copy link
Author

melvin-bot bot commented Nov 8, 2023

@sobitneupane 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!

@Christinadobrzyn
Copy link
Contributor

I think we need a bug teammate for payment? Let me know if that's true and I can add the label.

Copy link
Author

melvin-bot bot commented Nov 10, 2023

@sobitneupane 10 days overdue. I'm getting more depressed than Marvin.

1 similar comment
Copy link
Author

melvin-bot bot commented Nov 10, 2023

@sobitneupane 10 days overdue. I'm getting more depressed than Marvin.

@Christinadobrzyn Christinadobrzyn added the Bug Something is broken. Auto assigns a BugZero manager. label Nov 10, 2023
Copy link
Author

melvin-bot bot commented Nov 10, 2023

Triggered auto assignment to @joekaufmanexpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

This comment was marked as off-topic.

@Christinadobrzyn
Copy link
Contributor

adding BZ for payment and tracking this issue. I'm happy to take this Joe if you'd prefer.
@sobitneupane where are we with this? Can you update us?

@joekaufmanexpensify
Copy link
Contributor

I took a look at the PR, and it already has a separate payment issue, which @mallenexpensify is actively tracking.

@joekaufmanexpensify
Copy link
Contributor

Going to close this issue, since the PR is out on production, and Matt is handling the $250 to @sobitneupane in the separate issue linked above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2 External Added to denote the issue can be worked on by a contributor Reviewing Has a PR in review Task Typescript Migration
Projects
Development

No branches or pull requests

8 participants