This commit is contained in:
Azgaar 2020-06-23 02:29:39 +03:00
parent 7224addbc6
commit 300a05b75d
7 changed files with 231 additions and 212 deletions

163
libs/pell.js Normal file
View file

@ -0,0 +1,163 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.Pell = factory());
}(this, (function () { 'use strict';
const defaultParagraphSeparatorString = 'defaultParagraphSeparator'
const formatBlock = 'formatBlock'
const addEventListener = (parent, type, listener) => parent.addEventListener(type, listener)
const appendChild = (parent, child) => parent.appendChild(child)
const createElement = tag => document.createElement(tag)
const queryCommandState = command => document.queryCommandState(command)
const queryCommandValue = command => document.queryCommandValue(command)
const exec = (command, value = null) => document.execCommand(command, false, value)
const defaultActions = {
bold: {
icon: '<b>B</b>',
title: 'Bold',
state: () => queryCommandState('bold'),
result: () => exec('bold')
},
italic: {
icon: '<i>I</i>',
title: 'Italic',
state: () => queryCommandState('italic'),
result: () => exec('italic')
},
underline: {
icon: '<u>U</u>',
title: 'Underline',
state: () => queryCommandState('underline'),
result: () => exec('underline')
},
strikethrough: {
icon: '<strike>S</strike>',
title: 'Strike-through',
state: () => queryCommandState('strikeThrough'),
result: () => exec('strikeThrough')
},
heading1: {
icon: '<b>H<sub>1</sub></b>',
title: 'Heading 1',
result: () => exec(formatBlock, '<h1>')
},
heading2: {
icon: '<b>H<sub>2</sub></b>',
title: 'Heading 2',
result: () => exec(formatBlock, '<h2>')
},
paragraph: {
icon: '&#182;',
title: 'Paragraph',
result: () => exec(formatBlock, '<p>')
},
quote: {
icon: '&#8220; &#8221;',
title: 'Quote',
result: () => exec(formatBlock, '<blockquote>')
},
olist: {
icon: '&#35;',
title: 'Ordered List',
result: () => exec('insertOrderedList')
},
ulist: {
icon: '&#8226;',
title: 'Unordered List',
result: () => exec('insertUnorderedList')
},
code: {
icon: '&lt;/&gt;',
title: 'Code',
result: () => exec(formatBlock, '<pre>')
},
line: {
icon: '&#8213;',
title: 'Horizontal Line',
result: () => exec('insertHorizontalRule')
},
link: {
icon: '&#128279;',
title: 'Link',
result: () => navigator.clipboard.readText().then(url => exec('createLink', url))
},
image: {
icon: '&#128247;',
title: 'Image',
result: () => {
navigator.clipboard.readText().then(url => exec('insertImage', url))
exec('enableObjectResizing')
}
}
}
const defaultClasses = {
actionbar: 'pell-actionbar',
button: 'pell-button',
content: 'pell-content',
selected: 'pell-button-selected'
}
const init = settings => {
const actions = settings.actions
? (
settings.actions.map(action => {
if (typeof action === 'string') return defaultActions[action]
else if (defaultActions[action.name]) return { ...defaultActions[action.name], ...action }
return action
})
)
: Object.keys(defaultActions).map(action => defaultActions[action])
const classes = { ...defaultClasses, ...settings.classes }
const defaultParagraphSeparator = settings[defaultParagraphSeparatorString] || 'div'
const actionbar = createElement('div')
actionbar.className = classes.actionbar
appendChild(settings.element, actionbar)
const content = settings.element.content = createElement('div')
content.contentEditable = true
content.className = classes.content
content.oninput = ({ target: { firstChild } }) => {
if (firstChild && firstChild.nodeType === 3) exec(formatBlock, `<${defaultParagraphSeparator}>`)
else if (content.innerHTML === '<br>') content.innerHTML = ''
settings.onChange(content.innerHTML)
}
content.onkeydown = event => {
if (event.key === 'Enter' && queryCommandValue(formatBlock) === 'blockquote') {
setTimeout(() => exec(formatBlock, `<${defaultParagraphSeparator}>`), 0)
}
}
appendChild(settings.element, content)
actions.forEach(action => {
const button = createElement('button')
button.className = classes.button
button.innerHTML = action.icon
button.title = action.title
button.setAttribute('type', 'button')
button.onclick = () => action.result() && content.focus()
if (action.state) {
const handler = () => button.classList[action.state() ? 'add' : 'remove'](classes.selected)
addEventListener(content, 'keyup', handler)
addEventListener(content, 'mouseup', handler)
addEventListener(button, 'click', handler)
}
appendChild(actionbar, button)
})
if (settings.styleWithCSS) exec('styleWithCSS')
exec(defaultParagraphSeparatorString, defaultParagraphSeparator)
return settings.element
}
return {exec, init}
})));

View file

@ -1,188 +0,0 @@
// https://github.com/Highbrainer/jeevaneo-js-publicstorage. MIT
const IFRAME_ROOT_URL = "https://publicstorage.neocities.org/shared-iframe.html";
class PublicStorageAccess {
constructor ({debug=false}={}) {
this.uid = this.uniqueId();
this.debug=debug;
}
uniqueId() {
function chr4(){
return Math.random().toString(16).slice(-4);
}
return chr4() + chr4() +
'-' + chr4() +
'-' + chr4() +
'-' + chr4() +
'-' + chr4() + chr4() + chr4();
}
_debug(msg) {
if(this.debug) {
if(console && console.debug) {
console.debug(msg);
}
}
}
prepareIFrame() {
const that = this;
const iframe = document.createElement("iframe");
iframe.id=that.uid;
iframe.src=IFRAME_ROOT_URL + "?uid=init-"+that.uid;
iframe.style.cssText="display:none;";
return new Promise(function(resolve, reject) {
window.addEventListener('message', function mafunc(tkn) {
if (IFRAME_ROOT_URL.indexOf(tkn.origin)<0) {
return;
}
try {
const packet = JSON.parse(tkn.data);
if(!(packet.frameId === "init-" + that.uid)) {
// ignore
return;
}
if(packet.ready) {
resolve(iframe);
}
} catch (e) {
reject(tkn.data);
}
window.removeEventListener('message', mafunc);
});
onLoadThen().then(() => {
document.getElementsByTagName("body")[0].appendChild(iframe);
});
setTimeout(()=>reject(`Request ${that.uid} TIMEOUTED!`), 20000);
});
}
access(access, prop, value = null, level = "local") {
if(!(access === "get" || access === "set" || access === "delete")) {
throw new Error("access can only be 'set', 'get' or 'delete' - not '" + access + "'");
}
if (!prop) {
throw new Error("Prop name is mandatory");
}
if(!(level === "local" || level === "session")) {
throw new Error("level can only be 'session' or 'local' - not '" + access + "'");
}
const that = this;
const promise = new Promise(function(resolve, reject) {
that.prepareIFrame().then(iframe => {
window.addEventListener('message', function mafunc(tkn) {
if (IFRAME_ROOT_URL.indexOf(tkn.origin)<0) {
return;
}
try {
var packet = JSON.parse(tkn.data);
if(!(packet.uid === that.uid)) {
// ignore
return;
}
resolve(packet.body);
} catch (e) {
reject(tkn.data);
}
iframe.parentNode.removeChild(iframe);
window.removeEventListener('message', mafunc);
});
const request = {uid:that.uid, access:access, prop:prop, value:value, level:level};
iframe.contentWindow.postMessage(JSON.stringify(request), '*');
setTimeout(()=>reject("TIMEOUTED!"), 20000);
});
});
return promise;
}
}
function __createDebugIFrame() {
onLoadThen().then(function(){
const iframe = document.createElement("iframe");
iframe.src=IFRAME_ROOT_URL + "?for-debug-only";
iframe.style.cssText="display:none;";
document.getElementsByTagName("body")[0].appendChild(iframe);
});
}
class PublicStorage {
constructor({debug=false}={}) {
if(debug) {
__createDebugIFrame();
}
}
sessionGet(prop) {
return new PublicStorageAccess().access("get", prop, null, "session");
}
sessionSet(prop, value) {
return new PublicStorageAccess().access("set", prop, value, "session");
}
sessionUnset(prop) {
return new PublicStorageAccess().access("delete", prop, null, "session");
}
localGet(prop) {
return new PublicStorageAccess().access("get", prop, null, "local");
}
localSet(prop, value) {
return new PublicStorageAccess().access("set", prop, value, "local");
}
localUnset(prop) {
return new PublicStorageAccess().access("delete", prop, null, "local");
}
get(prop) {
return this.localGet(prop);
}
set(prop, value) {
return this.localSet(prop, value);
}
unset(prop) {
return this.localUnset(prop);
}
}
const publicstorage = new PublicStorage();
function onLoadThen() {
return new Promise(function(resolve, reject) {
if (window) {
if(document.getElementsByTagName('BODY')[0]) {
resolve();
} else {
registerOnLoad(function unregisterme() {
resolve();
window.removeEventListener('load', unregisterme);
});
}
}
setTimeout(function() {reject(new Error("Timeout waiting for onLoad!"));}, 10000);
});
}
function registerOnLoad(lambda) {
if (window.addEventListener) {
window.addEventListener('load', lambda);
} else if (window.attachEvent) {
window.attachEvent('onload', lambda);
}
}
onLoadThen().then(() => window.publicstorage = publicstorage).catch(e=> console.error(e));
//export {onLoadThen, PublicStorage, publicstorage as default}
// module.exports = onLoadThen();