xRyul / obsidian-image-converter

⚡️ Convert and compress images from one format to another by drag'n'dropping or pasting files into the note.
MIT License
111 stars 11 forks source link

change the time to register "create" event. support insert multi imgs at once. #24

Closed dotkt closed 6 months ago

dotkt commented 7 months ago

change the time to register "create" event. support insert multi imgs at once.

xRyul commented 7 months ago

Hey, thanks for contributing.

Following seems to allow multi drop and even saves a few ms during load. Many thanks 🙏 although, going to have to test it further, especially with various SYNC methods

        this.dropListener = () => {
            userAction = true;
            setTimeout(() => userAction = false, 10000);
        };

        this.app.workspace.onLayoutReady(() => {
            document.addEventListener("paste", this.pasteListener);
            document.addEventListener('drop', this.dropListener);
            this.registerEvent(
                this.app.vault.on('create', (file: TFile) => {
                    if (!(file instanceof TFile)) return;
                    if (isImage(file) && userAction) {
                        this.renameFile1(file);
                    }
                    // userAction = false; // uncommented to allow multi-drop
                })
            );
        })

        ...

        const currentLine = cursor.line;
        function escapeRegExp(string: string) {
            return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& 表示整个匹配的字符串
        }
        const findText = escapeRegExp(linkText);
        const replaceText = newLinkText;
        const docContent = editor.getValue();
        const regex = new RegExp(findText, 'g');
        const newContent = docContent.replace(regex, replaceText);
        editor.setValue(newContent);
        editor.setCursor({ line: currentLine, ch: 0 });
        // Ensure the current line is in a visible position
        editor.scrollIntoView({ from: { line: currentLine, ch: 0 }, to: { line: currentLine, ch: 0 } });
dotkt commented 7 months ago

npm run dev


发件人: xRyul @.> 发送时间: 2023年10月18日 19:10 收件人: xRyul/obsidian-image-converter @.> 抄送: dotkt @.>; Author @.> 主题: Re: [xRyul/obsidian-image-converter] change the time to register "create" event. support insert multi imgs at once. (PR #24)

Hey, thanks for contributing.

Following seems to allow multi drop and even saves a few ms during load. Many thanks 🙏 although, going to have to test it further, especially with various SYNC methods

            this.dropListener = () => {
                    userAction = true;
                    setTimeout(() => userAction = false, 10000);
            };

            this.app.workspace.onLayoutReady(() => {
                    document.addEventListener("paste", this.pasteListener);
                    document.addEventListener('drop', this.dropListener);
                    this.registerEvent(
                            this.app.vault.on('create', (file: TFile) => {
                                    if (!(file instanceof TFile)) return;
                                    if (isImage(file) && userAction) {
                                            this.renameFile1(file);
                                    }
                                    // userAction = false; // uncommented to allow multi-drop
                            })
                    );
            })

            ...

            const currentLine = cursor.line;
            function escapeRegExp(string: string) {
                    return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& 表示整个匹配的字符串
            }
            const findText = escapeRegExp(linkText);
            const replaceText = newLinkText;
            const docContent = editor.getValue();
            const regex = new RegExp(findText, 'g');
            const newContent = docContent.replace(regex, replaceText);
            editor.setValue(newContent);
            editor.setCursor({ line: currentLine, ch: 0 });
            // Ensure the current line is in a visible position
            editor.scrollIntoView({ from: { line: currentLine, ch: 0 }, to: { line: currentLine, ch: 0 } });

— Reply to this email directly, view it on GitHubhttps://github.com/xRyul/obsidian-image-converter/pull/24#issuecomment-1768224126, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AL3UF5R3VBTOSCQOJY5GDXTX762JPAVCNFSM6AAAAAA6AYLMJOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRYGIZDIMJSGY. You are receiving this because you authored the thread.Message ID: @.***>