index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import '../utils/dataTransfer/Clipboard.js';
  2. import { setLevelRef, ApiLevel } from '../utils/misc/level.js';
  3. import { wait } from '../utils/misc/wait.js';
  4. import { parseKeyDef } from './parseKeyDef.js';
  5. async function pointer(input) {
  6. const { pointerMap } = this.config;
  7. const actions = [];
  8. (Array.isArray(input) ? input : [
  9. input
  10. ]).forEach((actionInput)=>{
  11. if (typeof actionInput === 'string') {
  12. actions.push(...parseKeyDef(pointerMap, actionInput));
  13. } else if ('keys' in actionInput) {
  14. actions.push(...parseKeyDef(pointerMap, actionInput.keys).map((i)=>({
  15. ...actionInput,
  16. ...i
  17. })));
  18. } else {
  19. actions.push(actionInput);
  20. }
  21. });
  22. for(let i = 0; i < actions.length; i++){
  23. await wait(this.config);
  24. await pointerAction(this, actions[i]);
  25. }
  26. this.system.pointer.resetClickCount();
  27. }
  28. async function pointerAction(instance, action) {
  29. var _previousPosition_caret, _previousPosition_caret1;
  30. const pointerName = 'pointerName' in action && action.pointerName ? action.pointerName : 'keyDef' in action ? instance.system.pointer.getPointerName(action.keyDef) : 'mouse';
  31. const previousPosition = instance.system.pointer.getPreviousPosition(pointerName);
  32. var _action_target, _action_coords, _action_node, _action_offset;
  33. const position = {
  34. target: (_action_target = action.target) !== null && _action_target !== undefined ? _action_target : getPrevTarget(instance, previousPosition),
  35. coords: (_action_coords = action.coords) !== null && _action_coords !== undefined ? _action_coords : previousPosition === null || previousPosition === undefined ? undefined : previousPosition.coords,
  36. caret: {
  37. node: (_action_node = action.node) !== null && _action_node !== undefined ? _action_node : hasCaretPosition(action) ? undefined : previousPosition === null || previousPosition === undefined ? undefined : (_previousPosition_caret = previousPosition.caret) === null || _previousPosition_caret === undefined ? undefined : _previousPosition_caret.node,
  38. offset: (_action_offset = action.offset) !== null && _action_offset !== undefined ? _action_offset : hasCaretPosition(action) ? undefined : previousPosition === null || previousPosition === undefined ? undefined : (_previousPosition_caret1 = previousPosition.caret) === null || _previousPosition_caret1 === undefined ? undefined : _previousPosition_caret1.offset
  39. }
  40. };
  41. if ('keyDef' in action) {
  42. if (instance.system.pointer.isKeyPressed(action.keyDef)) {
  43. setLevelRef(instance, ApiLevel.Trigger);
  44. await instance.system.pointer.release(instance, action.keyDef, position);
  45. }
  46. if (!action.releasePrevious) {
  47. setLevelRef(instance, ApiLevel.Trigger);
  48. await instance.system.pointer.press(instance, action.keyDef, position);
  49. if (action.releaseSelf) {
  50. setLevelRef(instance, ApiLevel.Trigger);
  51. await instance.system.pointer.release(instance, action.keyDef, position);
  52. }
  53. }
  54. } else {
  55. setLevelRef(instance, ApiLevel.Trigger);
  56. await instance.system.pointer.move(instance, pointerName, position);
  57. }
  58. }
  59. function hasCaretPosition(action) {
  60. var _action_target, _ref;
  61. return !!((_ref = (_action_target = action.target) !== null && _action_target !== undefined ? _action_target : action.node) !== null && _ref !== undefined ? _ref : action.offset !== undefined);
  62. }
  63. function getPrevTarget(instance, position) {
  64. if (!position) {
  65. throw new Error('This pointer has no previous position. Provide a target property!');
  66. }
  67. var _position_target;
  68. return (_position_target = position.target) !== null && _position_target !== undefined ? _position_target : instance.config.document.body;
  69. }
  70. export { pointer };