4chan-x/src/Quotelinks/QuoteInline.js
Tuxedo Takodachi 567a1fe45e Squash and cleanup of the following:
- cca085e60090ca21edf0dee6aa012fc4c949809a
  start of import/export

- f816da146c32f010476872d15b58ec8301b9fdf2
  start of changing stuff until I can get a bundle

- c92adde147792356ff206107b2311590e8b2c054
  first bundle without errors

- e652dd2b785e355e0ac33566da7eaaaa19c7c539
  Bundling works with ts files

- 60fdb2539a757ca2f66258b21adf81246873893f
  meta info in compilation

- 8ccae783cbf65ac186d5669dedd9f945f7608694
  new build doesn't cause errors on page load as userscript

- 6fa11c42a05572779870f94b7ef4ea8dac373450
  work in progress: load userscript in browser and fix bugs

- b15c557d483de544a38a28cb78f25139d1d8421f
  migrated yotsuba templates to plain js

  the old templates caused some variable be in a wrong scope after
decaffeinate, causing them to be unreadable from the old template

  the old templates caused some variable be in a wrong scope after
decaffeinate, causing them to be unreadable from the old template

- 9d763e852fde74808ca14d5a8d6be45f51ae2765
  update readme

- 924eda8268bcfc4f1c0a83062ecd1d0d65bd92aa
  added more imports, and now the circular dependencies are haunting me

- ddd2d23315d801c7deaa28313833e667698aadd3
  jsx templates for escaped strings,
  more bug fixed from circular dependencies

- fee484dd447820d908c77b1e9d31235ab95a481c
  some fixes, clarify jsx

- e1d01d02eba5db2f604a5df786c525e95f32a2f9
  Unpacked extension
  more fixes

- 97d9090b712d20f7d851c82af84c65060f1a9c6e
  fixed class on post that caused catalog to appear empty

- 96a2c7b4a1e69f5812d1e53b2e4c90f6d8447b02
  A child class that's not supposed to run the parents constructor? That needs a workaround in es6 classes.

- fc06b4e1b2769550d4c69377b84d3ccacdb2e013
  changed jsx to make the tests pass

- 7b317b2a0feabe8caa547c76baf0c908b21592f1
  revert archive and banners to json
2023-03-15 21:01:03 +01:00

155 lines
4.9 KiB
JavaScript

import Callbacks from "../classes/Callbacks";
import Fetcher from "../classes/Fetcher";
import Get from "../General/Get";
import { g, Conf, doc } from "../globals/globals";
import ExpandComment from "../Miscellaneous/ExpandComment";
import Unread from "../Monitoring/Unread";
import $ from "../platform/$";
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
var QuoteInline = {
init() {
if (!['index', 'thread'].includes(g.VIEW) || !Conf['Quote Inlining']) { return; }
if (Conf['Comment Expansion']) {
ExpandComment.callbacks.push(this.node);
}
return Callbacks.Post.push({
name: 'Quote Inlining',
cb: this.node
});
},
node() {
const {process} = QuoteInline;
const {isClone} = this;
for (var link of this.nodes.quotelinks.concat([...Array.from(this.nodes.backlinks)], this.nodes.archivelinks)) {
process(link, isClone);
}
},
process(link, clone) {
if (Conf['Quote Hash Navigation']) {
if (!clone) { $.after(link, QuoteInline.qiQuote(link, $.hasClass(link, 'filtered'))); }
}
return $.on(link, 'click', QuoteInline.toggle);
},
qiQuote(link, hidden) {
let name = "hashlink";
if (hidden) { name += " filtered"; }
return $.el('a', {
className: name,
textContent: '#',
href: link.href
}
);
},
toggle(e) {
if ($.modifiedClick(e)) { return; }
const {boardID, threadID, postID} = Get.postDataFromLink(this);
if (Conf['Inline Cross-thread Quotes Only'] && (g.VIEW === 'thread') && g.posts.get(`${boardID}.${postID}`)?.nodes.root.offsetParent) { return; } // exists and not hidden
if ($.hasClass(doc, 'catalog-mode')) { return; }
e.preventDefault();
const quoter = Get.postFromNode(this);
const {context} = quoter;
if ($.hasClass(this, 'inlined')) {
QuoteInline.rm(this, boardID, threadID, postID, context);
} else {
if ($.x(`ancestor::div[@data-full-i-d='${boardID}.${postID}']`, this)) { return; }
QuoteInline.add(this, boardID, threadID, postID, context, quoter);
}
return this.classList.toggle('inlined');
},
findRoot(quotelink, isBacklink) {
if (isBacklink) {
return $.x('ancestor::*[parent::*[contains(@class,"post")]][1]', quotelink);
} else {
return $.x('ancestor-or-self::*[parent::blockquote][1]', quotelink);
}
},
add(quotelink, boardID, threadID, postID, context, quoter) {
let post;
const isBacklink = $.hasClass(quotelink, 'backlink');
const inline = $.el('div',
{className: 'inline'});
inline.dataset.fullID = `${boardID}.${postID}`;
const root = QuoteInline.findRoot(quotelink, isBacklink);
$.after(root, inline);
const qroot = $.x('ancestor::*[contains(@class,"postContainer")][1]', root);
$.addClass(qroot, 'hasInline');
new Fetcher(boardID, threadID, postID, inline, quoter);
if (!(
(post = g.posts.get(`${boardID}.${postID}`)) &&
(context.thread === post.thread)
)) { return; }
// Hide forward post if it's a backlink of a post in this thread.
// Will only unhide if there's no inlined backlinks of it anymore.
if (isBacklink && Conf['Forward Hiding']) {
$.addClass(post.nodes.root, 'forwarded');
post.forwarded++ || (post.forwarded = 1);
}
// Decrease the unread count if this post
// is in the array of unread posts.
if (!Unread.posts) { return; }
return Unread.readSinglePost(post);
},
rm(quotelink, boardID, threadID, postID, context) {
let el;
let inlined;
const isBacklink = $.hasClass(quotelink, 'backlink');
// Select the corresponding inlined quote, and remove it.
let root = QuoteInline.findRoot(quotelink, isBacklink);
root = $.x(`following-sibling::div[@data-full-i-d='${boardID}.${postID}'][1]`, root);
const qroot = $.x('ancestor::*[contains(@class,"postContainer")][1]', root);
const {parentNode} = root;
$.rm(root);
$.event('PostsRemoved', null, parentNode);
if (!$('.inline', qroot)) {
$.rmClass(qroot, 'hasInline');
}
// Stop if it only contains text.
if (!(el = root.firstElementChild)) { return; }
// Dereference clone.
const post = g.posts.get(`${boardID}.${postID}`);
post.rmClone(el.dataset.clone);
// Decrease forward count and unhide.
if (Conf['Forward Hiding'] &&
isBacklink &&
(context.thread === g.threads.get(`${boardID}.${threadID}`)) &&
!--post.forwarded) {
delete post.forwarded;
$.rmClass(post.nodes.root, 'forwarded');
}
// Repeat.
while ((inlined = $('.inlined', el))) {
({boardID, threadID, postID} = Get.postDataFromLink(inlined));
QuoteInline.rm(inlined, boardID, threadID, postID, context);
$.rmClass(inlined, 'inlined');
}
}
};
export default QuoteInline;