forked from AprilSylph/XKit-Rewritten
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.js
More file actions
146 lines (126 loc) · 5.59 KB
/
interface.js
File metadata and controls
146 lines (126 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { keyToCss } from './css_map.js';
import { dom } from './dom.js';
import { timelineSelector } from './timeline_id.js';
export const postSelector = '[tabindex="-1"][data-id]';
export const blogViewSelector = '[style*="--blog-title-color"] *';
export const notificationSelector = `:is(${keyToCss('notification')}[role="listitem"], ${keyToCss('activityItem')})`;
const listTimelineObjectSelector = keyToCss('listTimelineObject');
const cellSelector = keyToCss('cell');
const targetWrapperSelector = keyToCss(
'targetWrapper',
'targetWrapperBlock',
'targetWrapperFlex',
'targetWrapperInline'
);
/**
* @param {Element} element Element within a timeline item
* @returns {Element | null} The timeline item wrapper
*/
export const getTimelineItemWrapper = element =>
(element.closest('[data-timeline-id]') && element.closest(listTimelineObjectSelector)?.parentElement) ||
element.closest(cellSelector) ||
element.closest(listTimelineObjectSelector);
/**
* @param {Element} element Element within a popover wrapper
* @returns {Element | null} The outermost popover wrapper
*/
export const getPopoverWrapper = element => {
const closestWrapper = element.closest(targetWrapperSelector);
return closestWrapper?.parentElement?.matches(targetWrapperSelector)
? closestWrapper.parentElement
: closestWrapper;
};
/**
* @typedef {object} PostFilterOptions
* @property {string} [excludeClass] - Classname to exclude and add
* @property {Function|Function[]} [timeline] - Filter results to matching timeline element children
* @property {boolean} [noBlogView] - Whether to exclude posts in the blog view modal
* @property {boolean} [includeFiltered] - Whether to include filtered posts
*/
/**
* @param {Element[]} postElements - Post elements (or descendants) to filter
* @param {PostFilterOptions} [postFilterOptions] - Post filter options
* @returns {HTMLDivElement[]} Matching post elements
*/
export const filterPostElements = function (postElements, { excludeClass, timeline, noBlogView = false, includeFiltered = false } = {}) {
postElements = postElements
.filter(element => element.isConnected)
.map(element => element.closest(postSelector))
.filter(Boolean);
if (timeline) {
const timelineFilters = [timeline].flat().filter(Boolean);
postElements = postElements.filter(postElement => {
const timelineElement = postElement.closest(timelineSelector);
return (
timelineElement &&
timelineFilters.some(timelineFilter => timelineFilter(timelineElement))
);
});
}
if (noBlogView) {
postElements = postElements.filter(postElement => postElement.matches(blogViewSelector) === false);
}
if (!includeFiltered) {
postElements = postElements.filter(postElement => postElement.querySelector('article footer') !== null);
}
if (excludeClass) {
postElements = postElements.filter(({ classList }) => classList.contains(excludeClass) === false);
postElements.forEach(postElement => postElement.classList.add(excludeClass));
}
return postElements;
};
/**
* @param {PostFilterOptions} postFilterOptions - Post filter options
* @returns {HTMLDivElement[]} Matching post elements on the page
*/
export const getPostElements = postFilterOptions => filterPostElements([...document.querySelectorAll(postSelector)], postFilterOptions);
/**
* @param {string} [css] - CSS rules to be included
* @returns {HTMLStyleElement} Style element containing the provided CSS
*/
export const buildStyle = (css = '') => dom('style', { class: 'xkit' }, null, [css]);
/**
* Determine a post's legacy type
* @param {object} post - Destructured into content and layout
* @param {Array} [post.trail] - Full post trail
* @param {Array} [post.content] - Post content array
* @param {Array} [post.layout] - Post layout array
* @returns {string} The determined legacy type of the post
* @see https://github.com/tumblr/docs/blob/master/npf-spec.md#mapping-npf-post-content-to-legacy-post-types
*/
export const postType = ({ trail = [], content = [], layout = [] }) => {
content = trail[0]?.content || content;
layout = trail[0]?.layout || layout;
if (layout.some(({ type }) => type === 'ask')) return 'ask';
else if (content.some(({ type }) => type === 'video')) return 'video';
else if (content.some(({ type }) => type === 'image')) return 'photo';
else if (content.some(({ type }) => type === 'audio')) return 'audio';
else if (content.some(({ type, subtype }) => type === 'text' && subtype === 'quote')) return 'quote';
else if (content.some(({ type, subtype }) => type === 'text' && subtype === 'chat')) return 'chat';
else if (content.some(({ type }) => type === 'link')) return 'link';
else return 'text';
};
const getClosestWithOverflow = element => {
const parent = element.parentElement;
if (!parent) {
return element;
} else if (getComputedStyle(parent).overflowX !== 'visible') {
return parent;
} else {
return getClosestWithOverflow(parent);
}
};
export const appendWithoutOverflow = (element, target, defaultPosition = 'below') => {
element.className = defaultPosition;
element.style.removeProperty('--horizontal-offset');
target.appendChild(element);
const preventOverflowTarget = getClosestWithOverflow(target);
const preventOverflowTargetRect = preventOverflowTarget.getBoundingClientRect();
const elementRect = element.getBoundingClientRect();
if (elementRect.bottom > document.documentElement.clientHeight) {
element.className = 'above';
}
if (elementRect.right > preventOverflowTargetRect.right - 15) {
element.style.setProperty('--horizontal-offset', `${preventOverflowTargetRect.right - 15 - elementRect.right}px`);
}
};