Skip to content

Commit

Permalink
fix(main): Check selection on an interval instead of on selection change
Browse files Browse the repository at this point in the history
This both seems to fix the crashes on macOS (?) and also handles cases where, without a change of selection, page items get moved out of or into a page.
  • Loading branch information
pklaschka committed Aug 16, 2023
1 parent 47f0f0d commit b0da463
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions index.cjs
Expand Up @@ -6,7 +6,6 @@ const {
Group,
Application
} = require("indesign");
const { entrypoints } = require("uxp");
const { getOrCreateColor } = require("./lib/colors.cjs");

// more to get type definitions for the InDesign DOM than anything else:
Expand Down Expand Up @@ -36,30 +35,6 @@ Object.entries(buttons).forEach(([selector, flagFunction]) => {
});
});

/**
* @type {typeof EventListeners[number]}
*/
let listener = app.eventListeners.add('afterSelectionChanged', () => {
if (app.documents.length < 1)
console.log(`Selection change event triggered with no open documents.`);
else
console.log(`Selection changed: ${app.selection?.length || 0} items selected`);
checkSelection();
});

entrypoints.setup({
plugin: {
async create() {
console.log(`Plugin create called`);
},
async destroy() {
console.log(`Plugin destroy called`);
if (listener.isValid)
listener.remove();
}
}
})

function createPrideFlag(flagFunction, historyName = 'Add pride flag') {
if (checkSelection() === 'invalid')
return document.querySelector('#selection-error-dialog').showModal();
Expand Down Expand Up @@ -109,7 +84,7 @@ function createStripes(flagLocation, colors) {
const modeAlertContainer = document.querySelector('#selection-status-alert');
const mode = document.querySelector('#mode');

checkSelection();
setInterval(checkSelection, 500);

function checkSelection() {
if (app.documents.length < 1) {
Expand Down

0 comments on commit b0da463

Please sign in to comment.