diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1f25e..cc9c0bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,12 @@ - preserve styles when removing links (except Firefox) - prevent text nodes - i.e. leaves - as direct children of the content editable when user is typing +- `` and `
` do not need to be wrapped in a parent `contenteditable=false` anymore ### Fix - add link on Firefox +- clone image on drag # 0.0.4-1 (2022-02-08) diff --git a/src/events/input.events.ts b/src/events/input.events.ts index 54c2a5d..b267ff7 100644 --- a/src/events/input.events.ts +++ b/src/events/input.events.ts @@ -38,6 +38,13 @@ export class InputEvents { return; } + const {data} = $event; + + // User is not typing, for example an image is moved + if (data === null) { + return; + } + // User is typing text at the root of the container therefore the browser will create a text node a direct descendant of the contenteditable // This can happen when user types for example before or after an image diff --git a/src/index.html b/src/index.html index 49e80db..299234d 100644 --- a/src/index.html +++ b/src/index.html @@ -508,17 +508,17 @@

Another kind of rich text editor.

Stylo

Stylo is an open source WYSIWYG interactive editor for JavaScript. Its goal is to bring great user experience and interactivity to the web, for everyone, with no dependencies. Stylo is a DeckDeckGo project.
-

+
This is an ​alpha​ version! ⚠ī¸ The project is under active development and contributions on GitHub are most welcome.
-

+

Features

-

+
Pro tips: trigger bold mode with "**", italic mode with whitespace and "_", and mark mode with whitespace and "`". 👾
Cheers
David
-
+
diff --git a/src/menus/img.menu.ts b/src/menus/img.menu.ts index 5bc475b..2986c37 100644 --- a/src/menus/img.menu.ts +++ b/src/menus/img.menu.ts @@ -27,14 +27,10 @@ const setImageWith = ({ }: { size: '25%' | '50%' | '75%' | '100%'; paragraph: HTMLElement; -}) => { - const anchorImg: HTMLImageElement = paragraph.firstElementChild as HTMLImageElement; - anchorImg.style.setProperty('width', size); -}; +}) => paragraph.style.setProperty('width', size); export const imgMenu: StyloMenu = { - match: ({paragraph}: {paragraph: HTMLElement}) => - paragraph.firstElementChild?.nodeName.toLowerCase() === 'img', + match: ({paragraph}: {paragraph: HTMLElement}) => paragraph.nodeName.toLowerCase() === 'img', actions: [ { text: 'img_width_original', diff --git a/src/plugins/hr.plugin.ts b/src/plugins/hr.plugin.ts index 7428b47..a784742 100644 --- a/src/plugins/hr.plugin.ts +++ b/src/plugins/hr.plugin.ts @@ -1,5 +1,5 @@ import {StyloPlugin, StyloPluginCreateParagraphsParams} from '../types/plugin'; -import {createEmptyElement, createUneditableDiv} from '../utils/create-element.utils'; +import {createEmptyElement} from '../utils/create-element.utils'; import {transformParagraph} from '../utils/paragraph.utils'; export const hr: StyloPlugin = { @@ -8,11 +8,8 @@ export const hr: StyloPlugin = { createParagraphs: async ({container, paragraph}: StyloPluginCreateParagraphsParams) => { const hr: HTMLHRElement = document.createElement('hr'); - const element: HTMLDivElement = createUneditableDiv(); - element.append(hr); - transformParagraph({ - elements: [element, createEmptyElement({nodeName: 'div'})], + elements: [hr, createEmptyElement({nodeName: 'div'})], paragraph, container, focus: 'last' diff --git a/src/plugins/img.plugin.ts b/src/plugins/img.plugin.ts index 588d108..2808f7d 100644 --- a/src/plugins/img.plugin.ts +++ b/src/plugins/img.plugin.ts @@ -1,5 +1,5 @@ import {StyloPlugin, StyloPluginCreateParagraphsParams} from '../types/plugin'; -import {createEmptyElement, createUneditableDiv} from '../utils/create-element.utils'; +import {createEmptyElement} from '../utils/create-element.utils'; import {transformParagraph} from '../utils/paragraph.utils'; export const img: StyloPlugin = { @@ -17,13 +17,10 @@ export const img: StyloPlugin = { img.src = imgUrl; img.setAttribute('loading', 'lazy'); - const element: HTMLDivElement = createUneditableDiv(); - element.append(img); - const emptyDiv: HTMLElement = createEmptyElement({nodeName: 'div'}); transformParagraph({ - elements: [element, emptyDiv], + elements: [img, emptyDiv], paragraph, container, focus: 'last' diff --git a/src/utils/create-element.utils.spec.ts b/src/utils/create-element.utils.spec.ts index 59c0b7d..527e41f 100644 --- a/src/utils/create-element.utils.spec.ts +++ b/src/utils/create-element.utils.spec.ts @@ -1,4 +1,4 @@ -import {createEmptyElement, createUneditableDiv} from './create-element.utils'; +import {createEmptyElement} from './create-element.utils'; describe('create element', () => { it('should create empty element', () => { @@ -7,11 +7,4 @@ describe('create element', () => { expect(empty.nodeName.toLowerCase()).toEqual('div'); expect(empty.innerHTML).toEqual('\u200B'); }); - - it('should create a not editable element', () => { - const empty = createUneditableDiv(); - - expect(empty.nodeName.toLowerCase()).toEqual('div'); - expect(empty.getAttribute('contenteditable')).toEqual('false'); - }); }); diff --git a/src/utils/create-element.utils.ts b/src/utils/create-element.utils.ts index d8fa1aa..f72c4c8 100644 --- a/src/utils/create-element.utils.ts +++ b/src/utils/create-element.utils.ts @@ -8,17 +8,3 @@ export const createEmptyElement = ({ return element; }; - -// We do not want TextNode as direct child of the container. -// If user types next to some HTML elements, such as hr and img, the resulting text is a TextNode with the container as parent. -// -// -// -> after use type -// -// text -// -export const createUneditableDiv = (): HTMLDivElement => { - const div: HTMLDivElement = document.createElement('div'); - div.setAttribute('contenteditable', 'false'); - return div; -};