Skip to content

Commit

Permalink
table-input - Improve wrapping behavior (#7259)
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Brigante <[email protected]>
  • Loading branch information
ForsakenHarmony and fregante committed Mar 20, 2024
1 parent c3f0d42 commit 1c7574e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
45 changes: 17 additions & 28 deletions source/features/collapsible-content-button.tsx
Expand Up @@ -3,12 +3,12 @@ import FoldDownIcon from 'octicons-plain-react/FoldDown';
import * as pageDetect from 'github-url-detection';
import {insertTextIntoField} from 'text-field-edit';
import delegate, {DelegateEvent} from 'delegate-it';
import {elementExists} from 'select-dom';

import features from '../feature-manager.js';
import smartBlockWrap from '../helpers/smart-block-wrap.js';
import observe from '../helpers/selector-observer.js';
import {isHasSelectorSupported} from '../helpers/select-has.js';
import {triggerActionBarOverflow} from '../github-helpers/index.js';

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,29 +36,18 @@ function addContentToDetails({delegateTarget}: DelegateEvent<MouseEvent, HTMLBut
);
}

function addButtons(referenceButton: HTMLElement): void {
const classes
= elementExists('md-ref', referenceButton)
? [
'toolbar-item',
'btn-octicon',
'p-2',
'p-md-1',
'tooltipped',
'tooltipped-sw',
'rgh-collapsible-content-btn',
]
: [
'Button',
'Button--iconOnly',
'Button--invisible',
'Button--medium',
'tooltipped',
'tooltipped-sw',
'rgh-collapsible-content-btn',
];

referenceButton.after(
function append(container: HTMLElement): void {
const classes = [
'Button',
'Button--iconOnly',
'Button--invisible',
'Button--medium',
'tooltipped',
'tooltipped-sw',
'rgh-collapsible-content-btn',
];

container.append(
<button
type="button"
className={classes.join(' ')}
Expand All @@ -68,13 +57,13 @@ function addButtons(referenceButton: HTMLElement): void {
<FoldDownIcon/>
</button>,
);

triggerActionBarOverflow(container);
}

function init(signal: AbortSignal): void {
observe([
'md-ref', // TODO: Drop in June 2024
'.ActionBar-item:has([data-md-button=\'ref\'])',
], addButtons, {signal});
observe(
'[data-target="action-bar.itemContainer"]', append, {signal});
delegate('.rgh-collapsible-content-btn', 'click', addContentToDetails, {signal});
}

Expand Down
17 changes: 11 additions & 6 deletions source/features/table-input.tsx
Expand Up @@ -9,6 +9,7 @@ import features from '../feature-manager.js';
import smartBlockWrap from '../helpers/smart-block-wrap.js';
import observe from '../helpers/selector-observer.js';
import {isHasSelectorSupported} from '../helpers/select-has.js';
import {triggerActionBarOverflow} from '../github-helpers/index.js';

function addTable({delegateTarget: square}: DelegateEvent<MouseEvent, HTMLButtonElement>): void {
/* There's only one rich-text editor even when multiple fields are visible; the class targets it #5303 */
Expand All @@ -32,15 +33,15 @@ function addTable({delegateTarget: square}: DelegateEvent<MouseEvent, HTMLButton
field.selectionEnd = field.value.indexOf('<td>', cursorPosition) + '<td>'.length;
}

function add(anchor: HTMLElement): void {
function append(container: HTMLElement): void {
const wrapperClasses = [
'details-reset',
'details-overlay',
'flex-auto',
'select-menu',
'select-menu-modal-right',
'hx_rsm',
'float-left',
'ActionBar-item',
];

const buttonClasses = [
Expand All @@ -50,8 +51,11 @@ function add(anchor: HTMLElement): void {
'Button--medium',
];

anchor.after(
<details className={wrapperClasses.join(' ')}>
container.append(
<details
className={wrapperClasses.join(' ')}
data-targets="action-bar.items" // Enables automatic hiding when it doesn't fit
>
<summary
className={buttonClasses.join(' ')}
role="button"
Expand All @@ -68,7 +72,6 @@ function add(anchor: HTMLElement): void {
<details-menu
className="select-menu-modal position-absolute left-0 hx_rsm-modal rgh-table-input"
role="menu"
data-targets="action-bar.items" // Enables automatic hiding when it doesn't fit
>
{Array.from({length: 25}).map((_, index) => (
<button
Expand All @@ -84,10 +87,12 @@ function add(anchor: HTMLElement): void {
</details-menu>
</details>,
);

triggerActionBarOverflow(container);
}

function init(signal: AbortSignal): void {
observe('.ActionBar-item:has([data-md-button=\'ref\'])', add, {signal});
observe('[data-target="action-bar.itemContainer"]', append, {signal});
delegate('.rgh-tic', 'click', addTable, {signal});
}

Expand Down
7 changes: 7 additions & 0 deletions source/github-helpers/index.ts
Expand Up @@ -155,3 +155,10 @@ export function addAfterBranchSelector(branchSelectorParent: HTMLDetailsElement,
export function triggerRepoNavOverflow(): void {
window.dispatchEvent(new Event('resize'));
}

export function triggerActionBarOverflow(child: Element): void {
const parent = child.closest('action-bar')!;
const placeholder = document.createElement('div');
parent.replaceWith(placeholder);
placeholder.replaceWith(parent);
}

0 comments on commit 1c7574e

Please sign in to comment.