Browse Source

Move parser events

pull/6360/head
Artur Arseniev 2 years ago
parent
commit
de9c563abd
  1. 9
      packages/core/src/parser/index.ts
  2. 16
      packages/core/src/parser/model/ParserCss.ts
  3. 6
      packages/core/src/parser/model/ParserHtml.ts
  4. 25
      packages/core/src/parser/types.ts

9
packages/core/src/parser/index.ts

@ -25,14 +25,17 @@
* @module Parser
*/
import { Module } from '../abstract';
import { ObjectAny } from '../common';
import EditorModel from '../editor/model/Editor';
import defConfig, { HTMLParserOptions, ParserConfig } from './config/config';
import ParserCss from './model/ParserCss';
import ParserHtml from './model/ParserHtml';
import { ParserEvents } from './types';
export default class ParserModule extends Module<ParserConfig & { name?: string }> {
parserHtml: ReturnType<typeof ParserHtml>;
parserCss: ReturnType<typeof ParserCss>;
events = ParserEvents;
constructor(em: EditorModel) {
super(em, 'Parser', defConfig());
@ -85,5 +88,11 @@ export default class ParserModule extends Module<ParserConfig & { name?: string
return this.parserCss.parse(input);
}
__emitEvent(event: string, data: ObjectAny) {
const { em, events } = this;
em.trigger(event, data);
em.trigger(events.all, { event, ...data });
}
destroy() {}
}

16
packages/core/src/parser/model/ParserCss.ts

@ -3,6 +3,7 @@ import { CssRuleJSON } from '../../css_composer/model/CssRule';
import EditorModel from '../../editor/model/Editor';
import { ParsedCssRule, ParserConfig } from '../config/config';
import BrowserCssParser, { parseSelector, createNode } from './BrowserParserCss';
import { ParserEvents } from '../types';
const ParserCss = (em?: EditorModel, config: ParserConfig = {}) => ({
/**
@ -10,13 +11,22 @@ const ParserCss = (em?: EditorModel, config: ParserConfig = {}) => ({
* @param {String} input CSS string
* @return {Array<Object>}
*/
parse(input: string) {
parse(input: string, opts: { throwOnError?: boolean } = {}) {
let output: CssRuleJSON[] = [];
const { parserCss } = config;
const editor = em?.Editor;
const nodes = parserCss ? parserCss(input, editor!) : BrowserCssParser(input);
let nodes: CssRuleJSON[] | ParsedCssRule[] = [];
let error: unknown;
try {
nodes = parserCss ? parserCss(input, editor!) : BrowserCssParser(input);
} catch (err) {
error = err;
if (opts.throwOnError) throw err;
}
nodes.forEach((node) => (output = output.concat(this.checkNode(node))));
em?.trigger('parse:css', { input, output, nodes });
em?.Parser.__emitEvent(ParserEvents.css, { input, output, nodes, error });
return output;
},

6
packages/core/src/parser/model/ParserHtml.ts

@ -6,9 +6,9 @@ import { HTMLParseResult, HTMLParserOptions, ParseNodeOptions, ParserConfig } fr
import BrowserParserHtml from './BrowserParserHtml';
import { doctypeToString } from '../../utils/dom';
import { isDef } from '../../utils/mixins';
import { ParserEvents } from '../types';
const modelAttrStart = 'data-gjs-';
const event = 'parse:html';
const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boolean } = {}) => {
return {
@ -365,7 +365,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
if (styleStr) res.css = parserCss.parse(styleStr);
}
em?.trigger(`${event}:root`, { input, root: root });
em?.Parser.__emitEvent(ParserEvents.htmlRoot, { input, root });
let resHtml: HTMLParseResult['html'] = [];
if (asDocument) {
@ -379,7 +379,7 @@ const ParserHtml = (em?: EditorModel, config: ParserConfig & { returnArray?: boo
}
res.html = resHtml;
em?.trigger(event, { input, output: res, options });
em?.Parser.__emitEvent(ParserEvents.html, { input, output: res, options });
return res;
},

25
packages/core/src/parser/types.ts

@ -0,0 +1,25 @@
/**{START_EVENTS}*/
export enum ParserEvents {
/**
* @event `parse:html` On HTML parse, an object containing the input and the output of the parser is passed as an argument.
* @example
* editor.on('parse:html', ({ input, output }) => { ... });
*/
html = 'parse:html',
htmlRoot = 'parse:html:root',
/**
* @event `parse:css` On CSS parse, an object containing the input and the output of the parser is passed as an argument.
* @example
* editor.on('parse:css', ({ input, output }) => { ... });
*/
css = 'parse:css',
/**
* @event `parse` Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback.
* @example
* editor.on('parse', ({ event, ... }) => { ... });
*/
all = 'parse',
}
/**{END_EVENTS}*/
Loading…
Cancel
Save