diff --git a/src/editor/model/Editor.ts b/src/editor/model/Editor.ts index 24c69207a..1c4f241ba 100644 --- a/src/editor/model/Editor.ts +++ b/src/editor/model/Editor.ts @@ -10,6 +10,7 @@ import EditorModule from '..'; import EditorView from '../view/EditorView'; import { IModule } from '../../abstract/Module'; import CanvasModule from '../../canvas'; +import ComponentManager from '../../dom_components'; //@ts-ignore Backbone.$ = $; @@ -109,6 +110,10 @@ export default class EditorModel extends Model { return this.get('Editor'); } + get Components(): ComponentManager { + return this.get('DomComponents'); + } + constructor(conf = {}) { super(); this._config = conf; diff --git a/src/parser/config/config.ts b/src/parser/config/config.ts index 97861a889..46eb0d43d 100644 --- a/src/parser/config/config.ts +++ b/src/parser/config/config.ts @@ -1,3 +1,6 @@ +import { CssRuleProperties } from '../../css_composer/model/CssRule'; +import EditorModule from '../../editor'; + export interface HTMLParserOptions { /** * DOMParser mime type. @@ -18,6 +21,12 @@ export interface HTMLParserOptions { * @default false */ allowUnsafeAttr?: boolean; + + /** + * When false, removes empty text nodes when parsed, unless they contain a space. + * @default false + */ + keepEmptyTextNodes?: boolean; } export interface ParserConfig { @@ -31,7 +40,7 @@ export interface ParserConfig { * Custom CSS parser. * @see https://grapesjs.com/docs/guides/Custom-CSS-parser.html */ - parserCss?: any; // TODO + parserCss?: (str: string, editor: EditorModule) => CssRuleProperties[]; /** * Custom HTML parser. @@ -52,12 +61,13 @@ export interface ParserConfig { const config: ParserConfig = { textTags: ['br', 'b', 'i', 'u', 'a', 'ul', 'ol'], - parserCss: null, + parserCss: undefined, parserHtml: null, optionsHtml: { htmlType: 'text/html', allowScripts: false, allowUnsafeAttr: false, + keepEmptyTextNodes: false, }, }; diff --git a/src/parser/index.js b/src/parser/index.js deleted file mode 100644 index b21ff7aee..000000000 --- a/src/parser/index.js +++ /dev/null @@ -1,107 +0,0 @@ -/** - * You can customize the initial state of the module from the editor initialization, by passing the following [Configuration Object](https://github.com/artf/grapesjs/blob/master/src/parser/config/config.js) - * ```js - * const editor = grapesjs.init({ - * parser: { - * // options - * } - * }) - * ``` - * - * Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance - * - * ```js - * const { Parser } = editor; - * ``` - * ## Available Events - * * `parse:html` - On HTML parse, an object containing the input and the output of the parser is passed as an argument - * * `parse:css` - On CSS parse, an object containing the input and the output of the parser is passed as an argument - * - * ## Methods - * * [getConfig](#getconfig) - * * [parseHtml](#parsehtml) - * * [parseCss](#parsecss) - * - * @module Parser - */ -import defaults from './config/config'; -import parserCss from './model/ParserCss'; -import parserHtml from './model/ParserHtml'; - -export default () => { - let conf = {}; - let pHtml, pCss; - - return { - compTypes: '', - - parserCss: null, - - parserHtml: null, - - name: 'Parser', - - init(config = {}) { - conf = { ...defaults, ...config }; - conf.Parser = this; - pHtml = new parserHtml(conf); - pCss = new parserCss(conf); - this.em = conf.em; - this.parserCss = pCss; - this.parserHtml = pHtml; - return this; - }, - - /** - * Get the configuration object - * @returns {Object} Configuration object - * @example - * console.log(Parser.getConfig()) - */ - getConfig() { - return conf; - }, - - /** - * Parse HTML string and return the object containing the Component Definition - * @param {String} input HTML string to parse - * @param {Object} [options] Options - * @param {String} [options.htmlType] [HTML mime type](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02) to parse - * @param {Boolean} [options.allowScripts=false] Allow `