/g, (s,n)=>{
this.refs[n].s = this.htmlToText(s).trim();
return '';
});
});
this.prepareTextStep('removeBadTags', t => {
return t.replace(/<(?!\/?(h1|br|p|b|s|i|center|right)[\s|>])[^>]*>/g, '');
});
this.prepareTextStep('removeAttributes', t => {
return t.replace(/<(\/?)(h1|br|b|s|i|center|right)(?=[\s|>])[^>]*>/g, '<$1$2>');
});
this.prepareTextStep('split', t => {
return this.parts = t.split(/
]*>/).map(e=>e.split('
')).slice(1).map(([name, text])=>{
name = this.htmlToText(name).replace(/\s+/g, ' ').trim()
text = this.tabber(text);
let o = {
name,
text,
com: '',
comp: true,
refs: [],
};
text = text.replace(/\s*\/\*\s*([^]*?)\s*\*\/\s*/, (s,a,i,t)=>{
o.com = a;
o.comp = i > 100;
return '';
});
let supi = 1;
text = text.replace(/[⁰¹²³⁴⁵⁶⁷⁸⁹]+/g, (s,i,t)=>{
let n = fromsupnum(s);
let ref = refs[n];
ref.n = supi;
ref.t = tosupnum(supi);
o.refs.push(ref);
supi++;
return ref.t;
});
o.text = text;
return o;
});
});
this.prepareTextStep('join', t => {
return t
.map(p => {
let t = p.text.replace(/\n/g, '\n
');
if (p.com || p.refs.length) {
let com = p.com;
let ref = p.refs.map(r=>r.t + this.htmlToText(' ') + r.s).join('\n
');
let brc = com && ref ? '
' : '';
t = p.comp ? `${t}
${ref}${brc}${com}` : `
${com}${brc}${ref}${t}`;
}
return `
\n${p.name}\n
${t}\n`;
})
.join('\n');
});
this.prepareTextStep('display', t => {
return this.els.prepared.innerHTML = t;
});
this.prepared = true;
this.els.infoEditor.innerText = this.strings.sucPrepared;
this.els.infoEditor.classList.toggle('btn-info', false);
this.els.infoEditor.classList.toggle('btn-success', true);
this.makePreparedButtons();
}
makePreparedButtons() {
qq('.fu-summary').map(e => {
let name = e.getAttribute('chapter');
let chapter = this.parts.find(e => e.name == name);
let a = qq('.parts .title a').find(e=>e.innerText == name);
chapter.a = a;
chapter.summary = e;
console.log({chapter, a, name});
chapter.buttons = elm('.fu-summary-buttons').appendTo(e);
chapter.status = elm('sup.fu-chapter-status').appendTo(chapter.buttons);
if (chapter.a) {
elm('button.fu-sync-chapter', this.strings.chReload+this.strings.chDownload, click => {click.preventDefault(); this.syncChapter(chapter)})
.appendTo(chapter.buttons);
} else {
chapter.status.innerText = this.strings.statusTextFromTo(0, chapter.text.length);
elm('button.fu-chapter-make', this.strings.chUpload, click => {click.preventDefault(); this.makeChapter(chapter)})
.appendTo(chapter.buttons);
}
});
}
async syncChapter(chapter) {
console.log(chapter);
chapter.summary.q('.fu-sync-chapter').classList.add('fu-rotate');
console.log(window.chap = chapter);
if (!chapter.a) {
throw alert('wrong button!');
}
chapter.doc = await fetch.doc(chapter.a.href);
chapter.summary.q('.fu-sync-chapter').remove();
chapter.oldText = chapter.doc.q('textarea#content').value;
console.log(chapter);
chapter.isSame = chapter.text == chapter.oldText;
chapter.status.innerText =
chapter.isSame ? this.strings.statusTextSame: this.strings.statusTextFromTo(chapter.oldText.length, chapter.text.length);
chapter.btnUpdate = elm('button.fu-chapter-update', this.strings.chUpload, click=>this.updateChapter(chapter));
chapter.buttons.append(chapter.status);
if (!chapter.isSame) {
chapter.buttons.append(chapter.btnUpdate);
}
}
async updateChapter(chapter) {
console.log('upload', chapter);
chapter.buttons.q('.fu-chapter-update').classList.add('fu-rotate');
chapter.iframe = elm('iframe').appendTo(chapter.buttons);
await this.iframeLoad(chapter.iframe, chapter.a.href);
let ta = chapter.iframe.contentDocument.querySelector('textarea#content');
ta.scrollIntoView();
console.log('oldText: ', ta.value == chapter.oldText)
if (ta.value != chapter.oldText) {
alert('Error: can\'t update, chapter text has changed');
throw new Error('oldText has changed!');
}
await Promise.frame(30);
ta.value = chapter.text;
console.log('text: ', ta.value == chapter.text);
await Promise.frame(30);
let bsave = chapter.iframe.contentDocument.querySelector('#save_part')
bsave.scrollIntoView();
await Promise.frame(30);
await this.iframeLoad(chapter.iframe, () => bsave.click())
console.log('frame loaded, chapter updated!');
ta = chapter.iframe.contentDocument.querySelector('textarea#content');
chapter.newText = ta.value;
if (ta.value != chapter.text) {
alert('Error: failed to update dunno why');
throw new Error('failed to update dunno why!');
}
await Promise.frame(30);
chapter.status.innerText = this.strings.statusTextSame;
chapter.buttons.q('.fu-chapter-update').remove();
chapter.iframe.remove();
chapter.iframe = null;
}
async makeChapter(chapter) {
console.log('upload', window.chap=chapter);
chapter.buttons.q('.fu-chapter-make').classList.add('fu-rotate');
chapter.iframe = elm('iframe').appendTo(chapter.buttons);
let href = q('.add-part a[href*="addpart"]').href;
await this.iframeLoad(chapter.iframe, href);
let ta = chapter.iframe.contentDocument.querySelector('textarea#content');
let ti = chapter.iframe.contentDocument.querySelector('#titleInput');
let cb = chapter.iframe.contentDocument.querySelector('#not_published_chb');
ti.scrollIntoView();
ti.value = chapter.name;
await Promise.frame(30);
ta.scrollIntoView();
await Promise.frame(30);
ta.value = chapter.text;
await Promise.frame(30);
cb.scrollIntoView();
cb.checked = true;
await Promise.frame(30);
let bsave = chapter.iframe.contentDocument.querySelector('button[type="submit"]')
if (bsave?.innerText != 'Добавить часть') {
alert('Кнопка не найдена, нажмите сами');
}
bsave?.scrollIntoView();
await Promise.frame(30);
await this.iframeLoad(chapter.iframe, () => bsave?.click());
console.log('frame loaded, chapter updated!');
await Promise.frame(30);
chapter.status.innerText = this.strings.statusTextSame;
chapter.buttons.q('.fu-chapter-make').remove();
chapter.iframe.remove();
chapter.iframe = null;
}
async iframeLoad(iframe, src='') {
return new Promise(r => {
iframe.addEventListener('load', r);
if (src) {
if (typeof src == 'string') iframe.src = src;
if (typeof src == 'function') src(iframe);
}
});
}
htmlToText(h) {
let a = document.createElement('a');
a.innerHTML = h;
return a.innerText;
}
tosupnum(t) {
let num = '⁰¹²³⁴⁵⁶⁷⁸⁹'.split('');
return (t + '').match(/\d/g).map(e=>num[e]).join('');
}
fromsupnum(t) {
let num = '⁰¹²³⁴⁵⁶⁷⁸⁹'.split('');
return +(t + '').match(/[⁰¹²³⁴⁵⁶⁷⁸⁹]/g).map(e=>num.indexOf(e)).join('');
}
tabber(s) {
let hTexts = {};
function hText(s) {
if (hTexts[s])
return hTexts[s];
let a = document.createElement('a');
a.innerHTML = s;
return hTexts[s] = a.innerText;
}
const nbsp = '\xa0';
//hText(' ');
const emsp = '\u2003';
//hText(' ');
const ndash = '\u2013';
//hText('–');
const replacers = [
[/\n/g, ' '],
[/^\s+|\s+$/gm, '\n\n\n'],
[/&[^;]{2,7};/g, hText],
[/
|<.div>
]*>|<.div>|
]*>/g, '\n'],
[/
]*(center|right)[^>]*>([^]*?)<\/p>/g, '\n<$1>\n$2\n$1>\n'],
[/<\/p>\s*
]*>/g, '\n'],
[/<\/p>\s*|\s*
]*>/g, '\n'],
[/