mirror of
https://github.com/webislife/wc-wysiwyg.git
synced 2025-07-21 11:32:22 +00:00
@ -77,9 +77,12 @@ npm run build
|
|||||||
</wc-wysiwyg>
|
</wc-wysiwyg>
|
||||||
```
|
```
|
||||||
-->
|
-->
|
||||||
First, include JS and define custom element
|
First need integrate wc-wysiwyg styles, you have 2 way, vanila css in `dist/sass` or scss in `src/sass` just include in your web project
|
||||||
|
|
||||||
|
Second, include JS and define custom element
|
||||||
```javascript
|
```javascript
|
||||||
import('/src/components/wc-wysiwyg.js').then(esm => {
|
import('/src/components/wc-wysiwyg.js').then(esm => {
|
||||||
|
//you can pass any name into define fn
|
||||||
esm.define();
|
esm.define();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@webislife/wc-wysiwyg",
|
"name": "@webislife/wc-wysiwyg",
|
||||||
"version": "0.9.31",
|
"version": "0.9.32",
|
||||||
"description": "WYWSIWYG HTML5 Editor written in ts and designed by web-componennt, support all JS frameworks and browsers",
|
"description": "WYWSIWYG HTML5 Editor written in ts and designed by web-componennt, support all JS frameworks and browsers",
|
||||||
"main": "dist/wc-wysiwyg.js",
|
"main": "dist/wc-wysiwyg.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
@ -43,6 +43,7 @@ export const t = {
|
|||||||
minlength: 'Min length is:',
|
minlength: 'Min length is:',
|
||||||
maxlength: 'Max length is:',
|
maxlength: 'Max length is:',
|
||||||
filtertags: 'Found filter tag:',
|
filtertags: 'Found filter tag:',
|
||||||
|
details: 'Spoiler block',
|
||||||
},
|
},
|
||||||
ru: {
|
ru: {
|
||||||
h1:'Заголовок 1 уровня',
|
h1:'Заголовок 1 уровня',
|
||||||
@ -60,7 +61,7 @@ export const t = {
|
|||||||
b:'Жирный',
|
b:'Жирный',
|
||||||
i:'Курсив',
|
i:'Курсив',
|
||||||
u:'Подчеркнутый',
|
u:'Подчеркнутый',
|
||||||
s:'Маленький',
|
s:'Перечеркнутый',
|
||||||
sup:'Надстрочный',
|
sup:'Надстрочный',
|
||||||
sub:'Подстрочный',
|
sub:'Подстрочный',
|
||||||
kbd:'Кнопка',
|
kbd:'Кнопка',
|
||||||
@ -87,5 +88,6 @@ export const t = {
|
|||||||
minlength: 'Минимальная длинна:',
|
minlength: 'Минимальная длинна:',
|
||||||
maxlength: 'Максимальная длинна:',
|
maxlength: 'Максимальная длинна:',
|
||||||
filtertags: 'Найден запрещенный тег:',
|
filtertags: 'Найден запрещенный тег:',
|
||||||
|
details: 'Блок спойлера',
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -90,19 +90,10 @@ h5 {
|
|||||||
h6 {
|
h6 {
|
||||||
font-size: 1em;
|
font-size: 1em;
|
||||||
}
|
}
|
||||||
/* h1[id]::before,
|
|
||||||
h2[id]::before,
|
|
||||||
h3[id]::before,
|
|
||||||
h4[id]::before,
|
|
||||||
h5[id]::before,
|
|
||||||
h6[id]::before {
|
|
||||||
content: '§';
|
|
||||||
color: var(--color-blue-gray-300);
|
|
||||||
margin-right: 0.5em;
|
|
||||||
} */
|
|
||||||
/* del\ins */
|
/* del\ins */
|
||||||
del {
|
del {
|
||||||
color: var(--color-red-900);
|
color: var(--color-red-900);
|
||||||
|
border-bottom: 1px solid var(--color-red-900);
|
||||||
background-color: var(--color-red-50);
|
background-color: var(--color-red-50);
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
@ -119,6 +110,7 @@ del:before {
|
|||||||
}
|
}
|
||||||
ins {
|
ins {
|
||||||
color: var(--color-green-900);
|
color: var(--color-green-900);
|
||||||
|
border-bottom: 1px solid var(--color-green-900);
|
||||||
background-color: var(--color-green-50);
|
background-color: var(--color-green-50);
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@ const allTags = [
|
|||||||
{ tag: 'audio'},
|
{ tag: 'audio'},
|
||||||
{ tag: 'video'},
|
{ tag: 'video'},
|
||||||
{ tag: 'blockquote'},
|
{ tag: 'blockquote'},
|
||||||
|
{ tag: 'details'},
|
||||||
] as WCWYSIWYGTag[];
|
] as WCWYSIWYGTag[];
|
||||||
|
|
||||||
class WCWYSIWYG extends HTMLElement {
|
class WCWYSIWYG extends HTMLElement {
|
||||||
@ -136,7 +137,6 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
|
|
||||||
//allow inline without ['video','audio','img']
|
//allow inline without ['video','audio','img']
|
||||||
this.EditorInlineActions = this.EditorTags.filter(action => ['video','audio','img'].includes(action.tag) === false);
|
this.EditorInlineActions = this.EditorTags.filter(action => ['video','audio','img'].includes(action.tag) === false);
|
||||||
|
|
||||||
|
|
||||||
this.EditorActionsSection = el('section', { classList: ['wc-wysiwyg_ec'] });
|
this.EditorActionsSection = el('section', { classList: ['wc-wysiwyg_ec'] });
|
||||||
//Clear format button
|
//Clear format button
|
||||||
@ -202,9 +202,7 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Actions in footer
|
//Actions in footer
|
||||||
this.EditorBottomForm = el('fieldset', {
|
this.EditorBottomForm = el('fieldset', { classList: ['wc-wysiwyg_bt'] });
|
||||||
classList: ['wc-wysiwyg_bt'],
|
|
||||||
});
|
|
||||||
|
|
||||||
//Check custom tags
|
//Check custom tags
|
||||||
this.EditorCustomTags = JSON.parse( String(this.getAttribute('data-custom-tags')) );
|
this.EditorCustomTags = JSON.parse( String(this.getAttribute('data-custom-tags')) );
|
||||||
@ -224,60 +222,23 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
classList: ['wc-wysiwyg_content', this.getAttribute('data-content-class') || ''],
|
classList: ['wc-wysiwyg_content', this.getAttribute('data-content-class') || ''],
|
||||||
props: {
|
props: {
|
||||||
contentEditable: true,
|
contentEditable: true,
|
||||||
|
//Pointer event behaviors
|
||||||
onpointerup: event => {
|
onpointerup: event => {
|
||||||
this.checkCanClearElement(event);
|
this.#checkCanClearElement(event);
|
||||||
if(this.#EditProps) {
|
if(this.#EditProps) {
|
||||||
this.checkEditProps(event);
|
this.#checkEditProps(event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
//Update content on input event
|
||||||
oninput: event => {
|
oninput: event => {
|
||||||
this.updateContent();
|
this.updateContent();
|
||||||
if(this.#Autocomplete) {
|
if(this.#Autocomplete) {
|
||||||
this.checkAutoComplete();
|
this.#checkAutoComplete();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
//Handle key bindings
|
//Check hot keys is pressed
|
||||||
onkeydown: event => {
|
onkeydown: event => {
|
||||||
//check hold alt
|
this.#checkKeyBindings(event)
|
||||||
if(event.altKey) {
|
|
||||||
//alt+space - move caret to parent node next sibling
|
|
||||||
if(event.code === 'Space') {
|
|
||||||
const Selection = window.getSelection();
|
|
||||||
if(Selection?.type === 'Caret') {
|
|
||||||
//insertAdjacentElement dont support textNodes, first insert span
|
|
||||||
const span = el('span');
|
|
||||||
Selection?.anchorNode?.parentElement?.insertAdjacentElement('afterend', span)
|
|
||||||
//after replace span with textnode and select it
|
|
||||||
const textN = document.createTextNode(' ');
|
|
||||||
span.replaceWith(textN);
|
|
||||||
const range = document.createRange();
|
|
||||||
range.selectNodeContents(textN);
|
|
||||||
Selection.removeAllRanges();
|
|
||||||
Selection.addRange(range);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//tag - hide editor dialog
|
|
||||||
if(event.code === 'Escape') {
|
|
||||||
this.hideEditorInlineDialog();
|
|
||||||
}
|
|
||||||
//enter - set p as default tag in newline
|
|
||||||
if(event.code === 'Enter' && event.shiftKey === false) {
|
|
||||||
const Selection = window.getSelection();
|
|
||||||
let tagName = 'p';
|
|
||||||
//tags with return default browser behavior
|
|
||||||
if(['LI', 'ARTICLE', 'P'].includes(Selection.anchorNode.parentElement.tagName)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const p = el(tagName, { props: { innerHTML: ` ` } });
|
|
||||||
Selection?.anchorNode?.parentElement?.insertAdjacentElement('afterend', p);
|
|
||||||
const range = document.createRange();
|
|
||||||
range.selectNodeContents(p);
|
|
||||||
Selection?.removeAllRanges();
|
|
||||||
Selection?.addRange(range);
|
|
||||||
event.stopPropagation();
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -459,7 +420,7 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
/**
|
/**
|
||||||
* Check if need append autocompleted tags variants
|
* Check if need append autocompleted tags variants
|
||||||
*/
|
*/
|
||||||
checkAutoComplete() {
|
#checkAutoComplete() {
|
||||||
//CHeck autococmplete
|
//CHeck autococmplete
|
||||||
const Selecton = window.getSelection();
|
const Selecton = window.getSelection();
|
||||||
if(Selecton !== null && Selecton.anchorNode !== null) {
|
if(Selecton !== null && Selecton.anchorNode !== null) {
|
||||||
@ -523,10 +484,10 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checking and clear tag, if can do it
|
* Checking clear form and clear, if can do it
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
checkCanClearElement(event:Event) {
|
#checkCanClearElement(event:Event) {
|
||||||
const eventTarget = event.target as HTMLElement;
|
const eventTarget = event.target as HTMLElement;
|
||||||
if(eventTarget !== this.EditorNode) {
|
if(eventTarget !== this.EditorNode) {
|
||||||
if(eventTarget.nodeName !== 'P'
|
if(eventTarget.nodeName !== 'P'
|
||||||
@ -547,8 +508,7 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
/**
|
/**
|
||||||
* Checking click tag for editable props
|
* Checking click tag for editable props
|
||||||
**/
|
**/
|
||||||
checkEditProps(event) {
|
#checkEditProps(event) {
|
||||||
//Check need edit props
|
|
||||||
const eventTarget = event.target as HTMLElement;
|
const eventTarget = event.target as HTMLElement;
|
||||||
|
|
||||||
//Check exist prop\attr
|
//Check exist prop\attr
|
||||||
@ -598,7 +558,59 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#makeActionButtons(toEl:HTMLElement, actions) {
|
/**
|
||||||
|
* Cheking hot keys when keydown pressed
|
||||||
|
* @param event Keyboard event
|
||||||
|
*/
|
||||||
|
#checkKeyBindings(event:KeyboardEvent) {
|
||||||
|
//check hold alt
|
||||||
|
if(event.altKey) {
|
||||||
|
//alt+space - move caret to parent node next sibling
|
||||||
|
if(event.code === 'Space') {
|
||||||
|
const Selection = window.getSelection();
|
||||||
|
if(Selection?.type === 'Caret') {
|
||||||
|
//insertAdjacentElement dont support textNodes, first insert span
|
||||||
|
const span = el('span');
|
||||||
|
Selection?.anchorNode?.parentElement?.insertAdjacentElement('afterend', span)
|
||||||
|
//after replace span with textnode and select it
|
||||||
|
const textN = document.createTextNode(' ');
|
||||||
|
span.replaceWith(textN);
|
||||||
|
const range = document.createRange();
|
||||||
|
range.selectNodeContents(textN);
|
||||||
|
Selection.removeAllRanges();
|
||||||
|
Selection.addRange(range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//tag - hide editor dialog
|
||||||
|
if(event.code === 'Escape') {
|
||||||
|
this.hideEditorInlineDialog();
|
||||||
|
}
|
||||||
|
//enter - set p as default tag in newline
|
||||||
|
if(event.code === 'Enter' && event.shiftKey === false) {
|
||||||
|
const Selection = window.getSelection();
|
||||||
|
let tagName = 'p';
|
||||||
|
//tags with return default browser behavior
|
||||||
|
if(['LI', 'ARTICLE', 'P'].includes(Selection.anchorNode.parentElement.tagName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const p = el(tagName, { props: { innerHTML: ` ` } });
|
||||||
|
Selection?.anchorNode?.parentElement?.insertAdjacentElement('afterend', p);
|
||||||
|
const range = document.createRange();
|
||||||
|
range.selectNodeContents(p);
|
||||||
|
Selection?.removeAllRanges();
|
||||||
|
Selection?.addRange(range);
|
||||||
|
event.stopPropagation();
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make buttons and bind actions
|
||||||
|
* @param toEl htmlelement where append el
|
||||||
|
* @param actions
|
||||||
|
*/
|
||||||
|
#makeActionButtons(toEl:HTMLElement, actions:WCWYSIWYGTag[]) {
|
||||||
for (let i = 0; i < actions.length; i++) {
|
for (let i = 0; i < actions.length; i++) {
|
||||||
const action = actions[i];
|
const action = actions[i];
|
||||||
const button = el('button', {
|
const button = el('button', {
|
||||||
@ -607,14 +619,15 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
tabIndex: -1,
|
tabIndex: -1,
|
||||||
type:'button',
|
type:'button',
|
||||||
textContent: action.is ? `${action.tag} is=${action.is}` : action.tag,
|
textContent: action.is ? `${action.tag} is=${action.is}` : action.tag,
|
||||||
onpointerup: (event) => this.#tag(action.tag, event, action.is),
|
onpointerup: (event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
this.#tag(action)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
attrs: {
|
attrs: {
|
||||||
'data-hint': action.hint ? action.hint : this.#t(action.tag) || '-',
|
'data-hint': action.hint ? action.hint : this.#t(action.tag) || '-',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//-wc
|
|
||||||
//Make button with
|
|
||||||
toEl.appendChild(button);
|
toEl.appendChild(button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -623,44 +636,66 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
/**
|
/**
|
||||||
* Default behaviors fot tag actions
|
* Default behaviors fot tag actions
|
||||||
*/
|
*/
|
||||||
#tag = (tag:string, event:Event|false = false, is:boolean|string = false) => {
|
#tag = (tag:WCWYSIWYGTag) => {
|
||||||
switch (tag) {
|
switch (tag.tag) {
|
||||||
case 'audio':
|
case 'audio':
|
||||||
this.#Media('audio');
|
this.#Media('audio');
|
||||||
break;
|
break;
|
||||||
case 'video':
|
case 'video':
|
||||||
this.#Media('video');
|
this.#Media('video');
|
||||||
break;
|
break;
|
||||||
|
case 'details':
|
||||||
|
this.#Details();
|
||||||
|
break;
|
||||||
case 'img':
|
case 'img':
|
||||||
this.#Image();
|
this.#Image();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.#wrapTag(tag, is);
|
if(typeof tag.method === 'function') {
|
||||||
|
tag.method.apply(this, tag);
|
||||||
|
} else {
|
||||||
|
this.#wrapTag(tag, tag.is);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert spoiler
|
||||||
|
**/
|
||||||
|
#Details() {
|
||||||
|
const summaryTitle = prompt('Title', '');
|
||||||
|
if(summaryTitle === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const mediaEl = el('details', {
|
||||||
|
append: [
|
||||||
|
el('summary', { props: {innerText: summaryTitle} }),
|
||||||
|
el('p', { props: {innerText: '...'} })] }
|
||||||
|
);
|
||||||
|
this.EditorNode.append(mediaEl);
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap content in <tag>
|
* Wrap content in <tag>
|
||||||
**/
|
**/
|
||||||
#wrapTag = (tag, is:boolean|string = false) => {
|
#wrapTag = (tag, is:boolean|string = false) => {
|
||||||
const isList = ['ul', 'ol'].includes(tag);
|
const listTag = ['ul', 'ol'].includes(tag) ? tag : false;
|
||||||
|
tag = listTag !== false ? 'li' : tag;
|
||||||
const Selection = window.getSelection();
|
const Selection = window.getSelection();
|
||||||
let className = null;
|
let className = null;
|
||||||
let defaultOptions = {
|
let defaultOptions = {
|
||||||
classList: className ? className : undefined,
|
classList: className ? className : undefined,
|
||||||
} as any;
|
} as any;
|
||||||
if(isList) {
|
|
||||||
tag = 'li';
|
|
||||||
}
|
|
||||||
if(is) {
|
if(is) {
|
||||||
defaultOptions.options = {is};
|
defaultOptions.options = {is};
|
||||||
}
|
}
|
||||||
let tagNode = el(tag, defaultOptions);
|
let tagNode = el(tag, defaultOptions);
|
||||||
|
|
||||||
if (Selection !== null && Selection.rangeCount) {
|
if (Selection !== null && Selection.rangeCount) {
|
||||||
if(['ul', 'ol'].includes(tag)) {
|
if(listTag !== false) {
|
||||||
const list = el(tag);
|
const list = el(listTag);
|
||||||
tagNode.replaceWith(list);
|
tagNode.replaceWith(list);
|
||||||
list.append(tagNode)
|
list.append(tagNode)
|
||||||
}
|
}
|
||||||
@ -668,6 +703,7 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
range.surroundContents(tagNode);
|
range.surroundContents(tagNode);
|
||||||
Selection.removeAllRanges();
|
Selection.removeAllRanges();
|
||||||
Selection.addRange(range);
|
Selection.addRange(range);
|
||||||
|
//If selection has text, insert it
|
||||||
if(Selection.toString().length === 0) {
|
if(Selection.toString().length === 0) {
|
||||||
tagNode.innerText = tag;
|
tagNode.innerText = tag;
|
||||||
}
|
}
|
||||||
@ -703,15 +739,16 @@ class WCWYSIWYG extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(caption) {
|
if(caption) {
|
||||||
const figure = el('figure');
|
const figure = el('figure', {
|
||||||
|
append: [
|
||||||
const figcaption = el('figcaption', {
|
img,
|
||||||
props: {
|
el('figcaption', {
|
||||||
textContent: caption
|
props: {
|
||||||
}
|
textContent: caption
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]
|
||||||
});
|
});
|
||||||
figure.appendChild(img);
|
|
||||||
figure.appendChild(figcaption);
|
|
||||||
|
|
||||||
img.setAttribute('alt', caption);
|
img.setAttribute('alt', caption);
|
||||||
this.EditorNode.appendChild(figure);
|
this.EditorNode.appendChild(figure);
|
||||||
|
Reference in New Issue
Block a user