2021-12-10 02:37:01 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2021-12-10 02:37:01 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
import {ElementPosition} from 'protocol';
|
|
|
|
|
import {arrayEquals} from 'shared-utils';
|
|
|
|
|
|
|
|
|
|
import {ComponentTreeNode} from './interfaces';
|
2020-03-23 15:17:43 +00:00
|
|
|
|
|
|
|
|
interface ConsoleReferenceNode {
|
2021-12-09 05:44:17 +00:00
|
|
|
node: ComponentTreeNode | null;
|
2020-03-23 15:17:43 +00:00
|
|
|
position: ElementPosition;
|
|
|
|
|
}
|
2020-01-31 18:54:51 +00:00
|
|
|
|
2020-03-23 00:20:33 +00:00
|
|
|
const CONSOLE_REFERENCE_PREFIX = '$ng';
|
|
|
|
|
const CAPACITY = 5;
|
2020-03-22 20:38:57 +00:00
|
|
|
|
2020-03-23 15:17:43 +00:00
|
|
|
const nodesForConsoleReference: ConsoleReferenceNode[] = [];
|
|
|
|
|
|
|
|
|
|
export const setConsoleReference = (referenceNode: ConsoleReferenceNode) => {
|
|
|
|
|
if (referenceNode.node === null) {
|
2020-03-22 23:00:43 +00:00
|
|
|
return;
|
2020-03-22 20:38:57 +00:00
|
|
|
}
|
2020-03-23 15:17:43 +00:00
|
|
|
_setConsoleReference(referenceNode);
|
2020-03-22 20:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
2020-03-23 15:17:43 +00:00
|
|
|
const _setConsoleReference = (referenceNode: ConsoleReferenceNode) => {
|
|
|
|
|
prepareCurrentReferencesForInsertion(referenceNode);
|
|
|
|
|
nodesForConsoleReference.unshift(referenceNode);
|
2020-03-22 23:00:43 +00:00
|
|
|
assignConsoleReferencesFrom(nodesForConsoleReference);
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-23 15:17:43 +00:00
|
|
|
const prepareCurrentReferencesForInsertion = (referenceNode: ConsoleReferenceNode) => {
|
2021-12-09 05:44:17 +00:00
|
|
|
const foundIndex = nodesForConsoleReference.findIndex((nodeToLookFor) =>
|
|
|
|
|
arrayEquals(nodeToLookFor.position, referenceNode.position),
|
|
|
|
|
);
|
2020-03-22 23:00:43 +00:00
|
|
|
if (foundIndex !== -1) {
|
|
|
|
|
nodesForConsoleReference.splice(foundIndex, 1);
|
2020-03-23 00:20:33 +00:00
|
|
|
} else if (nodesForConsoleReference.length === CAPACITY) {
|
2020-03-22 23:00:43 +00:00
|
|
|
nodesForConsoleReference.pop();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-23 15:17:43 +00:00
|
|
|
const assignConsoleReferencesFrom = (referenceNodes: ConsoleReferenceNode[]) => {
|
2021-12-09 05:44:17 +00:00
|
|
|
referenceNodes.forEach((referenceNode, index) =>
|
|
|
|
|
setDirectiveKey(referenceNode.node, getConsoleReferenceWithIndexOf(index)),
|
|
|
|
|
);
|
2020-03-22 20:38:57 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-09 05:44:17 +00:00
|
|
|
const setDirectiveKey = (node: ComponentTreeNode | null, key: string) => {
|
2020-03-22 20:38:57 +00:00
|
|
|
Object.defineProperty(window, key, {
|
2020-01-31 18:54:51 +00:00
|
|
|
get: () => {
|
2020-04-01 19:58:31 +00:00
|
|
|
if (node?.component) {
|
|
|
|
|
return node.component.instance;
|
2020-02-02 03:54:35 +00:00
|
|
|
}
|
2020-04-01 19:58:31 +00:00
|
|
|
if (node?.nativeElement) {
|
2020-03-20 18:54:37 +00:00
|
|
|
return node.nativeElement;
|
|
|
|
|
}
|
|
|
|
|
return node;
|
2020-02-07 21:25:16 +00:00
|
|
|
},
|
|
|
|
|
configurable: true,
|
2020-01-31 18:54:51 +00:00
|
|
|
});
|
|
|
|
|
};
|
2020-03-23 00:20:33 +00:00
|
|
|
|
|
|
|
|
const getConsoleReferenceWithIndexOf = (consoleReferenceIndex: number) =>
|
2021-12-09 05:44:17 +00:00
|
|
|
`${CONSOLE_REFERENCE_PREFIX}${consoleReferenceIndex}`;
|