UNPKG

ngx-dynamic-hooks

Version:

Automatically insert live Angular components into a dynamic string of content (based on their selector or any pattern of your choice) and render the result in the DOM.

135 lines (134 loc) 5.56 kB
import { HookIndex } from '../../interfacesPublic'; import { HookParser, HookPosition } from '../../interfacesPublic'; import { AutoPlatformService } from '../platform/autoPlatformService'; import { ParseOptions } from '../settings/options'; import { Logger } from '../utils/logger'; import * as i0 from "@angular/core"; /** * An atomic replace instruction. Reads as: Replace the text from startIndex to endIndex with replacement. */ export interface ReplaceInstruction { startIndex: number; endIndex: number; replacement: string; } /** * Stores a HookPosition along with the parser who found it */ export interface ParserFindHooksResult { parser: HookParser; hookPosition: HookPosition; } /** * Stores the HookValue as well as the text surrounding it */ export interface HookSegments { enclosing: boolean; textBefore: string; openingTag: string; innerValue: string | null; closingTag: string | null; textAfter: string; } /** * The service responsible for finding text hooks in the content and replacing them with component anchors */ export declare class TextHookFinder { private platformService; private logger; constructor(platformService: AutoPlatformService, logger: Logger); /** * Finds all text hooks in an existing element and creates the corresponding anchors * * @param element - The element to parse * @param context - The current context object * @param parsers - The parsers to use * @param token - The current parse token * @param options - The current ParseOptions * @param hookIndex - The hookIndex object to fill */ findInElement(element: any, context: any, parsers: HookParser[], token: string, options: ParseOptions, hookIndex: HookIndex): void; /** * Checks an individual element and travels it recursively * * @param element - The element to parse * @param context - The current context object * @param parsers - The parsers to use * @param token - The current parse token * @param options - The current ParseOptions * @param hookIndex - The hookIndex object to fill * @param extractedNodes - A recursively-used object holding all temporarily extracted nodes */ checkElement(element: any, context: any, parsers: HookParser[], token: string, options: ParseOptions, hookIndex: HookIndex, extractedNodes?: { counter: number; nodes: { [key: string]: any; }; }): void; /** * Finds all text hooks in a string variable and creates the corresponding anchors * * @param content - The text to parse * @param context - The current context object * @param parsers - The parsers to use * @param token - The current parse token * @param options - The current ParseOptions * @param hookIndex - The hookIndex object to fill */ find(content: string, context: any, parsers: HookParser[], token: string, options: ParseOptions, hookIndex: HookIndex): { content: string; hookIndex: HookIndex; }; /** * Takes a HookPosition and returns the HookValue as well as the text surrounding it * * @param hookPosition - The HookPosition in question * @param content - The source text for the HookPosition */ private getHookSegments; /** * Checks the combined parserResults and validates them. Invalid ones are removed. * * @param parserResults - The parserResults to check * @param content - The content string * @param options - The current ParseOptions */ private validateHookPositions; /** * Outputs a warning in the console when the positions of two hooks are invalid in some manner * * @param message - The error message * @param hookPos - The first HookPosition * @param prevHookPos - The second HookPosition * @param content - The content string * @param options - The current ParseOptions */ private generateHookPosWarning; /** * When using an enclosing hook that is spread over several lines in an HTML editor, p-elements tend to get ripped apart. For example: * * <p><app-hook></p> * <h2>This is the hook content</h2> * <p></app-hook></p> * * would cause the innerValue of app-hook to have a lone closing and opening p-tag (as their counterparts are outside of the hook). * To clean up the HTML, this function removes a pair of these artifacts (e.g. <p> before hook, </p> inside hook) if BOTH are found. * This is important as the HTML parser will otherwise mess up the intended HTML and sometimes even put what should be ng-content below the component. * * @param firstText - The text on one side of the hook * @param firstArtifact - A string that should be removed from firstText... * @param firstRemoveIfAfter - ...if it appears after the last occurrence of this string * @param secondText - The text on the other side of the hook * @param secondArtifact - A string that should be removed from secondText... * @param secondRemoveIfBefore - ...if it appears before the first occurrence of this string */ private removeTagArtifacts; /** * Converts all HTML entities to normal characters * * @param text - The text with the potential HTML entities */ convertHTMLEntities(text: string): string; static ɵfac: i0.ɵɵFactoryDeclaration<TextHookFinder, never>; static ɵprov: i0.ɵɵInjectableDeclaration<TextHookFinder>; }