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

Save / Update of Front-Matter steals current cursor position and disrupted typing/editing #27

Open
sartian opened this issue Feb 28, 2025 · 1 comment

Comments

@sartian
Copy link

sartian commented Feb 28, 2025

First off, I like this plugin and find it very useful to make sure my docs always have up to date front-matter. 👍

I've had to disable it, unfortunately, because it makes editing documents nearly impossible, especially with table cells. :-/

When I have this plugin enabled and I'm trying to edit table cells in rich view I only have a few seconds before my edit cursor is yanked out of the cell I am editing and cursor focus is also lost so whatever I am writing in edit window is lost (Unless I am in source mode, doesn't happen then) .

I think it has something to do with periodic save and front-matter being updated loses cursor position / focus? I don't know if there is another plugin that conflicts with this one somehow to cause this problem. It could also be my update script itself that causes a problem, which I will include below...

If you have ideas or suggestions on how to resolve the issue or if there is something in my code that could be causing the problem...

To replicate the issue, if I am in the normal view and try to edit any table cell and I have this plugin enabled with my update code (below), I have about 2-3 seconds after I click into a sell and see my cursor edit there to type or modify the cell before the cursor disappears and focus in the edit window goes away... any additional typing just doesn't go anywhere.

Another odd issue is if I am typing at bottom of page and have extra empty rows at the bottom and I am typing, any whitespace at the bottom of the document will suddenly be truncated; I think it's somehow related to saving the updated front-matter.

Example table

My Super Complicated Table
NullPointer Exception strikes again! Debugging: Removing bugs, adding 10 more. Table not found. 404.
while(true) "Just one more feature..." "Works on my machine!" SQL joins at the hip.
Ctrl+C Ctrl+V "This is fine. flames everywhere" "Did you try turning it off and on?"

I'm open to any suggestions. I really like this plugin but it makes editing such a pain, especially if I am trying to edit a table.

my plugin update code

(() => {
  const crypto = require('crypto');
  const MODIFIED_WRITE_DELAY_TIME = 5000;
  const AUTO_SAVE_INTERVAL = 7000; // 7 seconds interval to ignore quick successive saves
  let timeout;

  function debounce(func, wait) {
    let timeout;
    return function(...args) {
      clearTimeout(timeout);
      const later = () => func.apply(this, args);
      timeout = setTimeout(later, wait);
      return later();
    };
  }

  function toLocalISO(date) {
    const tzOffset = date.getTimezoneOffset() * 60000; // offset in milliseconds
    const localTime = new Date(date.getTime() - tzOffset);
    const sign = tzOffset <= 0 ? '+' : '-';
    const pad = num => String(num).padStart(2, '0');
    const hours = pad(Math.floor(Math.abs(tzOffset) / 3600000));
    const minutes = pad(Math.abs(tzOffset % 3600000) / 60000);
    return `${localTime.toISOString().slice(0, -1)}${sign}${hours}:${minutes}`;
  }

  function generateUUID(ctime) {
    const hash = crypto.createHash('sha256').update(ctime.toString()).digest('hex');
    return [
      hash.slice(0, 8),
      hash.slice(8, 12),
      '4' + hash.slice(13, 16),
      ((parseInt(hash.slice(16, 17), 16) & 0x3) | 0x8).toString(16) + hash.slice(17, 20),
      hash.slice(20, 32)
    ].join('-');
  }

  function shouldSkipUpdate(lastModifiedTime) {
    const now = Date.now();
    return (now - lastModifiedTime) < AUTO_SAVE_INTERVAL;
  }

  function getUpdatedProperties(file) {
    const currentCtime = new Date(file.stat.ctime);
    const currentMtime = new Date(file.stat.mtime);
    const lastModifiedTime = file.properties?.Modified ? new Date(file.properties.Modified).getTime() : 0;

    if (shouldSkipUpdate(lastModifiedTime)) {
      return {};
    }

    const updatedProperties = {
      Title: file.basename,
      Created: file.properties?.Created || toLocalISO(currentCtime),
      Modified: toLocalISO(currentMtime),
      Folder: file.parent.path,
      UUID: file.properties?.UUID || generateUUID(currentCtime.getTime())
    };

    if (!file.properties?.Modified || currentMtime.getTime() > (lastModifiedTime + MODIFIED_WRITE_DELAY_TIME)) {
      updatedProperties.Modified = toLocalISO(currentMtime);
    }

    return updatedProperties;
  }

  const debouncedGetUpdatedProperties = debounce((file) => {
    return getUpdatedProperties(file);
  }, MODIFIED_WRITE_DELAY_TIME);

  return debouncedGetUpdatedProperties(file);
})();
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

No branches or pull requests

1 participant