Add coexistence checks to all enqueue methods to prevent loading both React and Grid.js assets simultaneously. Changes: - ReactAdmin.php: Only enqueue React assets when ?react=1 - Init.php: Skip Grid.js when React active on admin pages - Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0 - Customer.php, Product.php, License.php: Add coexistence checks Now the toggle between Classic and React versions works correctly. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
134 lines
4.9 KiB
JavaScript
134 lines
4.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.pressKeys = exports.setClipboardData = void 0;
|
|
/**
|
|
* External dependencies
|
|
*/
|
|
const change_case_1 = require("change-case");
|
|
/**
|
|
* WordPress dependencies
|
|
*/
|
|
const keycodes_1 = require("@wordpress/keycodes");
|
|
let clipboardDataHolder = {
|
|
'text/plain': '',
|
|
'text/html': '',
|
|
'rich-text': '',
|
|
};
|
|
/**
|
|
* Sets the clipboard data that can be pasted with
|
|
* `pressKeys( 'primary+v' )`.
|
|
*
|
|
* @param this
|
|
* @param clipboardData
|
|
* @param clipboardData.plainText
|
|
* @param clipboardData.html
|
|
*/
|
|
function setClipboardData({ plainText = '', html = '' }) {
|
|
clipboardDataHolder = {
|
|
'text/plain': plainText,
|
|
'text/html': html,
|
|
'rich-text': '',
|
|
};
|
|
}
|
|
exports.setClipboardData = setClipboardData;
|
|
async function emulateClipboard(page, type) {
|
|
clipboardDataHolder = await page.evaluate(([_type, _clipboardData]) => {
|
|
const canvasDoc =
|
|
// @ts-ignore
|
|
document.activeElement?.contentDocument ?? document;
|
|
const event = new ClipboardEvent(_type, {
|
|
bubbles: true,
|
|
cancelable: true,
|
|
clipboardData: new DataTransfer(),
|
|
});
|
|
if (!event.clipboardData) {
|
|
throw new Error('ClipboardEvent.clipboardData is null');
|
|
}
|
|
if (_type === 'paste') {
|
|
event.clipboardData.setData('text/plain', _clipboardData['text/plain']);
|
|
event.clipboardData.setData('text/html', _clipboardData['text/html']);
|
|
event.clipboardData.setData('rich-text', _clipboardData['rich-text']);
|
|
}
|
|
else {
|
|
const selection = canvasDoc.defaultView.getSelection();
|
|
const plainText = selection.toString();
|
|
let html = plainText;
|
|
if (selection.rangeCount) {
|
|
const range = selection.getRangeAt(0);
|
|
const fragment = range.cloneContents();
|
|
html = Array.from(fragment.childNodes)
|
|
.map((node) => node.outerHTML ??
|
|
node.nodeValue)
|
|
.join('');
|
|
}
|
|
event.clipboardData.setData('text/plain', plainText);
|
|
event.clipboardData.setData('text/html', html);
|
|
}
|
|
canvasDoc.activeElement.dispatchEvent(event);
|
|
return {
|
|
'text/plain': event.clipboardData.getData('text/plain'),
|
|
'text/html': event.clipboardData.getData('text/html'),
|
|
'rich-text': event.clipboardData.getData('rich-text'),
|
|
};
|
|
}, [type, clipboardDataHolder]);
|
|
}
|
|
const isAppleOS = () => process.platform === 'darwin';
|
|
const isWebkit = (page) => page.context().browser().browserType().name() === 'webkit';
|
|
const browserCache = new WeakMap();
|
|
const getHasNaturalTabNavigation = async (page) => {
|
|
if (!isAppleOS() || !isWebkit(page)) {
|
|
return true;
|
|
}
|
|
if (browserCache.has(page.context().browser())) {
|
|
return browserCache.get(page.context().browser());
|
|
}
|
|
const testPage = await page.context().newPage();
|
|
await testPage.setContent(`<button>1</button><button>2</button>`);
|
|
await testPage.getByText('1').focus();
|
|
await testPage.keyboard.press('Tab');
|
|
const featureDetected = await testPage
|
|
.getByText('2')
|
|
.evaluate((node) => node === document.activeElement);
|
|
browserCache.set(page.context().browser(), featureDetected);
|
|
await testPage.close();
|
|
return featureDetected;
|
|
};
|
|
const modifiers = {
|
|
...keycodes_1.modifiers,
|
|
shiftAlt: (_isApple) => _isApple() ? [keycodes_1.SHIFT, keycodes_1.ALT] : [keycodes_1.SHIFT, keycodes_1.CTRL],
|
|
};
|
|
async function pressKeys(key, { times, ...pressOptions } = {}) {
|
|
const hasNaturalTabNavigation = await getHasNaturalTabNavigation(this.page);
|
|
let command;
|
|
if (key.toLowerCase() === 'primary+c') {
|
|
command = () => emulateClipboard(this.page, 'copy');
|
|
}
|
|
else if (key.toLowerCase() === 'primary+x') {
|
|
command = () => emulateClipboard(this.page, 'cut');
|
|
}
|
|
else if (key.toLowerCase() === 'primary+v') {
|
|
command = () => emulateClipboard(this.page, 'paste');
|
|
}
|
|
else {
|
|
const keys = key.split('+').flatMap((keyCode) => {
|
|
if (Object.prototype.hasOwnProperty.call(modifiers, keyCode)) {
|
|
return modifiers[keyCode](isAppleOS).map((modifier) => modifier === keycodes_1.CTRL ? 'Control' : (0, change_case_1.capitalCase)(modifier));
|
|
}
|
|
else if (keyCode === 'Tab' && !hasNaturalTabNavigation) {
|
|
return ['Alt', 'Tab'];
|
|
}
|
|
return keyCode;
|
|
});
|
|
const normalizedKeys = keys.join('+');
|
|
command = () => this.page.keyboard.press(normalizedKeys);
|
|
}
|
|
times = times ?? 1;
|
|
for (let i = 0; i < times; i += 1) {
|
|
await command();
|
|
if (times > 1 && pressOptions.delay) {
|
|
await this.page.waitForTimeout(pressOptions.delay);
|
|
}
|
|
}
|
|
}
|
|
exports.pressKeys = pressKeys;
|
|
//# sourceMappingURL=press-keys.js.map
|