diff --git a/example/example-dev.html b/example/example-dev.html index 8064cb07..373385db 100644 --- a/example/example-dev.html +++ b/example/example-dev.html @@ -69,12 +69,12 @@ - + - + @@ -131,11 +131,11 @@ */ image: SimpleImage, - list: { - class: NestedList, - inlineToolbar: true, - shortcut: 'CMD+SHIFT+L' - }, + // list: { + // class: NestedList, + // inlineToolbar: true, + // shortcut: 'CMD+SHIFT+L' + // }, checklist: { class: Checklist, @@ -176,12 +176,12 @@ raw: RawTool, embed: Embed, - - table: { - class: Table, - inlineToolbar: true, - shortcut: 'CMD+ALT+T' - }, + // + // table: { + // class: Table, + // inlineToolbar: true, + // shortcut: 'CMD+ALT+T' + // }, }, @@ -194,126 +194,220 @@ * Initial Editor data */ data: { - blocks: [ + "time" : 1634414421172, + "blocks" : [ { - id: "zcKCF1S7X8", - type: "header", - data: { - text: "Editor.js", - level: 2 + "id" : "zcKCF1S7X8", + "type" : "header", + "data" : { + "text" : "Editor.js", + "level" : 2 } }, { - "id": "b6ji-DvaKb", - "type": "paragraph", - "data": { - "text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration." + "id" : "b6ji-DvaKb", + "type" : "paragraph", + "data" : { + "text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text. Source code of the page contains the example of connection and configuration." } }, { - type: "header", - id: "7ItVl5biRo", - data: { - text: "Key features", - level: 3 + "id" : "7ItVl5biRo", + "type" : "header", + "data" : { + "text" : "Key features", + "level" : 3 } }, { - type : 'list', - id: "SSBSguGvP7", - data : { - items : [ + "id" : "SSBSguGvP7", + "type" : "list", + "data" : { + "items" : [ { - content: 'It is a block-styled editor', - items: [] + "content" : "It is a block-styled editor", + "items" : [] }, { - content: 'It returns clean data output in JSON', - items: [] + "content" : "It returns clean data output in JSON", + "items" : [] }, { - content: 'Designed to be extendable and pluggable with a simple API', - items: [] + "content" : "Designed to be extendable and pluggable with a simple API", + "items" : [] } ], - style: 'unordered' + "style" : "unordered" } }, { - type: "header", - id: "QZFox1m_ul", - data: { - text: "What does it mean «block-styled editor»", - level: 3 + "id" : "QZFox1m_ul", + "type" : "header", + "data" : { + "text" : "What does it mean «block-styled editor»", + "level" : 3 } }, { - type : 'paragraph', - id: "bwnFX5LoX7", - data : { - text : 'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.' + "id" : "bwnFX5LoX7", + "type" : "paragraph", + "data" : { + "text" : "Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core." + }, + "fragments" : { + "text" : [ + { + "range" : [ + 123, + 210 + ], + "element" : "MARK", + "attributes" : { + "class" : "cdx-marker" + } + } + ] } }, { - type : 'paragraph', - id: "mTrPOHAQTe", - data : { - text : `There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.` + "id" : "mTrPOHAQTe", + "type" : "paragraph", + "data" : { + "text" : "There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games." + }, + "fragments" : { + "text" : [ + { + "range" : [ + 20, + 39 + ], + "element" : "A", + "attributes" : { + "href" : "https://github.com/editor-js" + } + }, + { + "range" : [ + 48, + 58 + ], + "element" : "A", + "attributes" : { + "href" : "https://editorjs.io/creating-a-block-tool" + } + } + ] } }, { - type: "header", - id: "1sYMhUrznu", - data: { - text: "What does it mean clean data output", - level: 3 + "id" : "1sYMhUrznu", + "type" : "header", + "data" : { + "text" : "What does it mean clean data output", + "level" : 3 } }, { - type : 'paragraph', - id: "jpd7WEXrJG", - data : { - text : 'Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below' + "id" : "jpd7WEXrJG", + "type" : "paragraph", + "data" : { + "text" : "Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below" } }, { - type : 'paragraph', - id: "0lOGNUKxqt", - data : { - text : `Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on.` + "id" : "0lOGNUKxqt", + "type" : "paragraph", + "data" : { + "text" : "Given data can be used as you want: render with HTML for Web clients, render natively for mobile apps, create markup for Facebook Instant Articles or Google AMP, generate an audio version and so on." + }, + "fragments" : { + "text" : [ + { + "range" : [ + 57, + 68 + ], + "element" : "CODE", + "attributes" : { + "class" : "inline-code" + } + }, + { + "range" : [ + 90, + 101 + ], + "element" : "CODE", + "attributes" : { + "class" : "inline-code" + } + }, + { + "range" : [ + 121, + 146 + ], + "element" : "CODE", + "attributes" : { + "class" : "inline-code" + } + }, + { + "range" : [ + 150, + 160 + ], + "element" : "CODE", + "attributes" : { + "class" : "inline-code" + } + }, + { + "range" : [ + 174, + 187 + ], + "element" : "CODE", + "attributes" : { + "class" : "inline-code" + } + } + ] } }, { - type : 'paragraph', - id: "WvX7kBjp0I", - data : { - text : 'Clean data is useful to sanitize, validate and process on the backend.' + "id" : "WvX7kBjp0I", + "type" : "paragraph", + "data" : { + "text" : "Clean data is useful to sanitize, validate and process on the backend." } }, { - type : 'delimiter', - id: "H9LWKQ3NYd", - data : {} + "id" : "H9LWKQ3NYd", + "type" : "delimiter", + "data" : {} }, { - type : 'paragraph', - id: "h298akk2Ad", - data : { - text : 'We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏' + "id" : "h298akk2Ad", + "type" : "paragraph", + "data" : { + "text" : "We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make its core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏" } }, { - type: 'image', - id: "9802bjaAA2", - data: { - url: 'assets/codex2x.png', - caption: '', - stretched: false, - withBorder: true, - withBackground: false, + "id" : "9802bjaAA2", + "type" : "image", + "data" : { + "url" : "assets/codex2x.png", + "caption" : "", + "withBorder" : true, + "withBackground" : false, + "stretched" : false } - }, - ] + } + ], + "version" : "2.23.0-rc.0" }, onReady: function(){ saveButton.click(); diff --git a/package.json b/package.json index f96813aa..b928617a 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "terser-webpack-plugin": "^2.3.6", "ts-loader": "^7.0.1", "tslint": "^6.1.1", - "typescript": "3.8.3", + "typescript": "4.4.3", "webpack": "^4.43.0", "webpack-cli": "^3.3.11" }, diff --git a/src/components/modules/blockManager.ts b/src/components/modules/blockManager.ts index 10da5ef2..3db05f27 100644 --- a/src/components/modules/blockManager.ts +++ b/src/components/modules/blockManager.ts @@ -15,6 +15,7 @@ import { BlockToolData, PasteEvent } from '../../../types'; import { BlockTuneData } from '../../../types/block-tunes/block-tune-data'; import BlockAPI from '../block/api'; import { BlockMutationType } from '../../../types/events/block/mutation-type'; +import { InlineFragment, InlineFragmentsDict, SavedData } from '../../../types/data-formats'; /** * @typedef {BlockManager} BlockManager @@ -268,6 +269,7 @@ export default class BlockManager extends Module { needToFocus = true, replace = false, tunes = {}, + fragments, }: { id?: string; tool?: string; @@ -276,6 +278,7 @@ export default class BlockManager extends Module { needToFocus?: boolean; replace?: boolean; tunes?: {[name: string]: BlockTuneData}; + fragments?: InlineFragmentsDict; } = {}): Block { let newIndex = index; @@ -283,6 +286,10 @@ export default class BlockManager extends Module { newIndex = this.currentBlockIndex + (replace ? 0 : 1); } + if (fragments !== undefined) { + data = this.insertInlineFragments(data, fragments); + } + const block = this.composeBlock({ id, tool, @@ -318,6 +325,89 @@ export default class BlockManager extends Module { return block; } + /** + * + * @param data + * @param fragmentsDict + * @private + */ + public insertInlineFragments(data: Pick, fragmentsDict: InlineFragmentsDict): Pick { + const insertToString = (str: string, fragments: InlineFragment[]): string => { + const template = document.createElement('template'); + + template.innerHTML = str; + + const walker = document.createTreeWalker(template.content, NodeFilter.SHOW_TEXT); + + const root = walker.currentNode; + + fragments.forEach(fragment => { + let offset = 0; + let startOffset = 0; + let endOffset = 0; + let startNode: Node | null = null; + let endNode: Node | null = null; + + while (!startNode || !endNode) { + const node = walker.nextNode(); + + if (!startNode && offset + node.textContent.length > fragment.range[0]) { + startNode = node; + startOffset = fragment.range[0] - offset; + } + + if (!endNode && offset + node.textContent.length >= fragment.range[1]) { + endNode = node; + endOffset = fragment.range[1] - offset; + } + + offset += node.textContent.length; + } + + const range = new Range(); + + range.setStart(startNode, startOffset); + range.setEnd(endNode, endOffset); + + const element = document.createElement(fragment.element); + + Object.entries(fragment.attributes).map(([name, value]) => element.setAttribute(name, value as string)); + + range.surroundContents(element); + + walker.currentNode = root; + }); + + return template.innerHTML; + }; + + const insert = (dataToProcess: Record, fragments: InlineFragmentsDict): Pick => { + Object + .entries(dataToProcess) + .forEach(([key, value]) => { + if (Array.isArray(value)) { + dataToProcess[key] = value.map(v => insert(v, fragments[key] as InlineFragmentsDict)); + + return; + } + + if (typeof value === 'object') { + dataToProcess[key] = insert(value as Record, fragments[key] as InlineFragmentsDict); + + return; + } + + if (typeof value === 'string') { + dataToProcess[key] = insertToString(value, fragments[key] as InlineFragment[]); + } + }); + + return dataToProcess as Pick; + }; + + return insert(data, fragmentsDict); + } + /** * Replace current working block * diff --git a/src/components/modules/renderer.ts b/src/components/modules/renderer.ts index 9aad79ff..a4fe507e 100644 --- a/src/components/modules/renderer.ts +++ b/src/components/modules/renderer.ts @@ -73,7 +73,7 @@ export default class Renderer extends Module { */ public async insertBlock(item: OutputBlockData): Promise { const { Tools, BlockManager } = this.Editor; - const { type: tool, data, tunes, id } = item; + const { type: tool, data, tunes, id, fragments } = item; if (Tools.available.has(tool)) { try { @@ -82,6 +82,7 @@ export default class Renderer extends Module { tool, data, tunes, + fragments, }); } catch (error) { _.log(`Block «${tool}» skipped because of plugins error`, 'warn', data); diff --git a/src/components/modules/saver.ts b/src/components/modules/saver.ts index 59725f65..5219cd54 100644 --- a/src/components/modules/saver.ts +++ b/src/components/modules/saver.ts @@ -7,10 +7,11 @@ */ import Module from '../__module'; import { OutputData } from '../../../types'; -import { SavedData, ValidatedData } from '../../../types/data-formats'; +import { InlineFragment, InlineFragmentsDict, SavedData, ValidatedData } from '../../../types/data-formats'; import Block from '../block'; import * as _ from '../utils'; -import { sanitizeBlocks } from '../utils/sanitizer'; +import $ from '../dom'; +import { deepSanitize, sanitizeBlocks } from '../utils/sanitizer'; declare const VERSION: string; @@ -43,11 +44,25 @@ export default class Saver extends Module { }); const extractedData = await Promise.all(chainData) as Array>; - const sanitizedData = await sanitizeBlocks(extractedData, (name) => { + const sanitizedData = sanitizeBlocks(extractedData, (name) => { return Tools.blockTools.get(name).sanitizeConfig; }); + const withFragments = sanitizedData.map(savedData => { + if (savedData.tool === this.Editor.Tools.stubTool) { + return savedData; + } - return this.makeOutput(sanitizedData); + const fragments = this.extractInlineFragments(savedData.data); + + savedData.data = deepSanitize(savedData.data, {}); + + return { + ...savedData, + fragments, + }; + }); + + return this.makeOutput(withFragments); } catch (e) { _.logLabeled(`Saving failed due to the Error %o`, 'error', e); } finally { @@ -83,7 +98,7 @@ export default class Saver extends Module { _.log('[Editor.js saving]:', 'groupCollapsed'); - allExtractedData.forEach(({ id, tool, data, tunes, time, isValid }) => { + allExtractedData.forEach(({ id, tool, data, tunes, fragments, time, isValid }) => { totalTime += time; /** @@ -116,6 +131,9 @@ export default class Saver extends Module { ...!_.isEmpty(tunes) && { tunes, }, + ...!_.isEmpty(fragments) && { + fragments, + }, }; blocks.push(output); @@ -130,4 +148,85 @@ export default class Saver extends Module { version: VERSION, }; } + + /** + * + * @param data + * @private + */ + private extractInlineFragments(data: Pick): InlineFragmentsDict { + const extractFromString = (str: string): InlineFragment[] => { + const template = $.make('template') as HTMLTemplateElement; + + template.innerHTML = str; + + const walker = document.createTreeWalker(template.content, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT); + + let node: Node | null; + let offset = 0; + const fragments: InlineFragment[] = []; + + while ((node = walker.nextNode()) !== null) { + switch (node.nodeType) { + case Node.TEXT_NODE: + offset += node.textContent.length; + break; + case Node.ELEMENT_NODE: { + const length = node.textContent.length; + const fragment: InlineFragment = { + range: [offset, offset + length], + element: node.nodeName, + attributes: Object.fromEntries(Array.from((node as HTMLElement).attributes).map(attr => ([attr.nodeName, attr.nodeValue]))), + }; + + fragments.push(fragment); + + break; + } + } + } + + return fragments; + }; + + const extract = (obj: Record): InlineFragmentsDict => { + const result: InlineFragmentsDict = {}; + + Object + .entries(obj) + .forEach(([key, value]) => { + if (Array.isArray(value)) { + const fragments = value.map(v => extract(v)).filter(fragment => Object.keys(fragment).length > 0); + + if (fragments.length > 0) { + result[key] = fragments; + } + + return; + } + + if (typeof value === 'object') { + const fragments = extract(obj); + + if (Object.keys(fragments).length > 0) { + result[key] = fragments; + } + + return; + } + + if (typeof value === 'string') { + const fragments = extractFromString(value); + + if (fragments.length > 0) { + result[key] = fragments; + } + } + }); + + return result; + }; + + return extract(data); + } } diff --git a/src/components/utils/sanitizer.ts b/src/components/utils/sanitizer.ts index 5b4bc8a2..55cdd6b1 100644 --- a/src/components/utils/sanitizer.ts +++ b/src/components/utils/sanitizer.ts @@ -87,7 +87,7 @@ export function clean(taintString: string, customConfig: SanitizerConfig = {} as * @param {BlockToolData|object|*} dataToSanitize - taint string or object/array that contains taint string * @param {SanitizerConfig} rules - object with sanitizer rules */ -function deepSanitize(dataToSanitize: object | string, rules: SanitizerConfig): object | string { +export function deepSanitize(dataToSanitize: object | string, rules: SanitizerConfig): object | string { /** * BlockData It may contain 3 types: * - Array diff --git a/tsconfig.json b/tsconfig.json index c95c063b..67923b68 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "target": "es2017", "declaration": false, "moduleResolution": "node", // This resolution strategy attempts to mimic the Node.js module resolution mechanism at runtime - "lib": ["dom", "es2017", "es2018"], + "lib": ["dom", "es2017", "es2018", "ES2019"], // allows to import .json files for i18n "resolveJsonModule": true, diff --git a/types/data-formats/index.d.ts b/types/data-formats/index.d.ts index 410f3653..f1f143a7 100644 --- a/types/data-formats/index.d.ts +++ b/types/data-formats/index.d.ts @@ -1,2 +1,3 @@ export * from './block-data'; +export * from './inline-fragments'; export * from './output-data'; diff --git a/types/data-formats/inline-fragments.d.ts b/types/data-formats/inline-fragments.d.ts new file mode 100644 index 00000000..a4b54bf8 --- /dev/null +++ b/types/data-formats/inline-fragments.d.ts @@ -0,0 +1,9 @@ +export interface InlineFragment { + range: [number, number]; + tool?: string; + element: string; + attributes?: Record; +} + +export type InlineFragmentsDict = {[key: string]: InlineFragment[] | InlineFragmentsDict | InlineFragmentsDict[]}; + diff --git a/types/data-formats/output-data.d.ts b/types/data-formats/output-data.d.ts index 07f296e1..c1d58707 100644 --- a/types/data-formats/output-data.d.ts +++ b/types/data-formats/output-data.d.ts @@ -1,5 +1,6 @@ import {BlockToolData} from '../tools'; import {BlockTuneData} from '../block-tunes/block-tune-data'; +import {InlineFragmentsDict} from './inline-fragments'; /** * Output of one Tool @@ -25,6 +26,8 @@ export interface OutputBlockData