Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions source/features/collapsible-content-button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'dom-chef';
import select from 'select-dom';
import {FoldDownIcon} from '@primer/octicons-react';
import * as pageDetect from 'github-url-detection';
import * as textFieldEdit from 'text-field-edit';
Expand All @@ -8,6 +7,7 @@ import delegate, {DelegateEvent} from 'delegate-it';
import features from '.';
import smartBlockWrap from '../helpers/smart-block-wrap';
import {onCommentEdit} from '../github-events/on-fragment-load';
import {attachElements} from '../helpers/attach-element';

function addContentToDetails({delegateTarget}: DelegateEvent<MouseEvent, HTMLButtonElement>): void {
/* There's only one rich-text editor even when multiple fields are visible; the class targets it #5303 */
Expand Down Expand Up @@ -36,14 +36,15 @@ function addContentToDetails({delegateTarget}: DelegateEvent<MouseEvent, HTMLBut
}

function addButtons(): void {
for (const anchor of select.all('md-ref:not(.rgh-collapsible-content-btn-added)')) {
anchor.classList.add('rgh-collapsible-content-btn-added');
anchor.after(
<button type="button" className="toolbar-item btn-octicon p-2 p-md-1 tooltipped tooltipped-sw rgh-collapsible-content-btn" aria-label="Add collapsible content">
attachElements({
anchor: 'md-ref',
className: 'rgh-collapsible-content-btn',
after: () => (
<button type="button" className="toolbar-item btn-octicon p-2 p-md-1 tooltipped tooltipped-sw" aria-label="Add collapsible content">
<FoldDownIcon/>
</button>,
);
}
</button>
),
});
}

function init(signal: AbortSignal): void {
Expand All @@ -56,6 +57,6 @@ void features.add(import.meta.url, {
include: [
pageDetect.hasRichTextEditor,
],
deduplicate: 'has-rgh-inner',
deduplicate: false,
init,
});
25 changes: 21 additions & 4 deletions source/helpers/attach-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ type Attachment<NewElement extends Element> = RequireAtLeastOne<{
allowMissingAnchor?: boolean;
}, Position>;

// Get a unique position in the code
type Attachments<NewElement extends Element> = Attachment<NewElement> & {
anchor: string;
};

/**
Get unique ID by using the line:column of the call (or its parents) as seed. Every call from the same place will return the same ID, as long as the index is set to the parents that matters to you.

@param index The line of the Error#stack generated inside this function
*/
function getSnapshotUUID(index = 3): string {
return hashString(new Error('Get stack').stack!.split('\n')[index]);
}
Expand All @@ -30,18 +38,18 @@ export default function attachElement<NewElement extends Element>({
before,
after,
allowMissingAnchor = false,
}: Attachment<NewElement>): NewElement[] | void {
}: Attachment<NewElement>): NewElement[] {
const anchorElement = typeof anchor === 'string' ? select(anchor) : anchor;
if (!anchorElement) {
if (allowMissingAnchor) {
return;
return [];
}

throw new Error('Element not found');
}

if (select.exists('.' + className, anchorElement.parentElement ?? anchorElement)) {
return;
return [];
}

const call = (position: Position, create: () => NewElement): NewElement => {
Expand All @@ -59,3 +67,12 @@ export default function attachElement<NewElement extends Element>({
// eslint-disable-next-line unicorn/no-array-callback-reference -- It only works this way. TS AMIRITE
].filter(isDefined);
}

export function attachElements<NewElement extends Element>({
anchor: anchors,
className = 'rgh-' + getSnapshotUUID(),
...options
}: Attachments<NewElement>): NewElement[] {
return select.all(`:is(${anchors}):not(.${className})`)
.flatMap(anchor => attachElement({...options, anchor, className}));
}