Repository: 4ian/GDevelop
File: newIDE/app/src/EventsSheet/ClipboardKind.js (JavaScript)

Code before:
    clipboardContent,
    'actionsCount'
  );
  if (actionsCount === null) return false;

  return actionsCount > 0;
};

export const copySelectionToClipboard = (selection: SelectionState) => {
  const eventsList = new gd.EventsList();
  const actionsList = new gd.InstructionsList();
  const conditionsList = new gd.InstructionsList();

  getSelectedEvents(selection).forEach(event =>
    eventsList.insertEvent(event, eventsList.getEventsCount())
  );
  getSelectedInstructionsContexts(selection).forEach(instructionContext => {
    if (instructionContext.isCondition) {
      conditionsList.insert(
        instructionContext.instruction,
        conditionsList.size()
      );
    } else {
      actionsList.insert(instructionContext.instruction, actionsList.size());
    }
  });

  Clipboard.set(CLIPBOARD_KIND, {
    eventsList: serializeToJSObject(eventsList),
    eventsCount: eventsList.getEventsCount(),
    actionsList: serializeToJSObject(actionsList),
    actionsCount: actionsList.size(),
    conditionsList: serializeToJSObject(conditionsList),
    conditionsCount: conditionsList.size(),
  });

  eventsList.delete();
  actionsList.delete();
  conditionsList.delete();
};

export const pasteEventsFromClipboardInSelection = (
  project: gdProject,
  selection: SelectionState
): boolean => {
  const lastSelectEventContext = getLastSelectedEventContext(selection);
  if (!lastSelectEventContext || !hasClipboardEvents()) return false;

  const clipboardContent = Clipboard.get(CLIPBOARD_KIND);
  const eventsListContent = SafeExtractor.extractArrayProperty(
    clipboardContent,

Review comment (suggestion):
Not sure if useful, but could you use contains: 

https://github.com/4ian/GDevelop/blob/6377ece6f97ec3dc4d323066afdab8b28ff82ea6/Core/GDCore/Events/EventsList.h#L138-L144

(maybe not because you don't have an EventsList).

Code after:
    clipboardContent,
    'actionsCount'
  );
  if (actionsCount === null) return false;

  return actionsCount > 0;
};

const addAllSubEvents = (
  event: gdBaseEvent,
  events: Set<gdBaseEvent>
): void => {
  const subEvents = event.getSubEvents();
  for (let i = 0; i < subEvents.getEventsCount(); i++) {
    const subEvent = subEvents.getEventAt(i);
    events.add(subEvent);
    addAllSubEvents(subEvent, events);
  }
};

const excludeEventsChildren = (
  events: Array<gdBaseEvent>
): Array<gdBaseEvent> => {
  const filteredEvents: Array<gdBaseEvent> = [];
  const allChildren = new Set<gdBaseEvent>();
  // Children may come first because the selection is ordered by selection time.
  for (const event of events) {
    if (!allChildren.has(event)) {
      addAllSubEvents(event, allChildren);
    }
  }
  for (const event of events) {
    if (!allChildren.has(event)) {
      filteredEvents.push(event);
    }
  }
  return filteredEvents;
};

export const copySelectionToClipboard = (selection: SelectionState) => {
  const eventsList = new gd.EventsList();
  const actionsList = new gd.InstructionsList();
  const conditionsList = new gd.InstructionsList();

  excludeEventsChildren(getSelectedEvents(selection)).forEach(event =>
    eventsList.insertEvent(event, eventsList.getEventsCount())
  );
  getSelectedInstructionsContexts(selection).forEach(instructionContext => {
    if (instructionContext.isCondition) {
      conditionsList.insert(
        instructionContext.instruction,