Skip to content

Commit

Permalink
Updates autolinks section
Browse files Browse the repository at this point in the history
- saves expanded state for the autolinks pane
- adds a count of autolinks in the pane header
  • Loading branch information
d13 committed Aug 15, 2022
1 parent 984f280 commit 7f17d81
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/storage.ts
Expand Up @@ -120,6 +120,7 @@ export const enum WorkspaceStorageKeys {
ViewsRepositoriesAutoRefresh = 'gitlens:views:repositories:autoRefresh',
ViewsSearchAndCompareKeepResults = 'gitlens:views:searchAndCompare:keepResults',
ViewsSearchAndComparePinnedItems = 'gitlens:views:searchAndCompare:pinned',
ViewsCommitDetailsAutolinksExpanded = 'gitlens:views:commitDetails:autolinksExpanded',

Deprecated_DisallowConnectionPrefix = 'gitlens:disallow:connection:',
Deprecated_PinnedComparisons = 'gitlens:pinned:comparisons',
Expand Down
1 change: 1 addition & 0 deletions src/webviews/apps/commitDetails/commitDetails.html
Expand Up @@ -73,6 +73,7 @@

<webview-pane collapsable expanded data-region="rich-pane">
<span slot="title">Autolinks</span>
<span slot="subtitle" data-region="autolink-count"></span>
<div class="commit-details__rich" data-region="rich-info" aria-hidden="true">
<p>
<code-icon icon="info"></code-icon>&nbsp;Use autolinks to identify external references, like
Expand Down
30 changes: 26 additions & 4 deletions src/webviews/apps/commitDetails/commitDetails.ts
Expand Up @@ -15,10 +15,12 @@ import {
OpenFileOnRemoteCommandType,
PickCommitCommandType,
PinCommitCommandType,
PreferencesCommandType,
SearchCommitCommandType,
} from '../../commitDetails/protocol';
import { App } from '../shared/appBase';
import type { FileChangeItem, FileChangeItemEventDetail } from '../shared/components/commit/file-change-item';
import type { WebviewPane, WebviewPaneExpandedChangeEventDetail } from '../shared/components/webview-pane';
import { DOM } from '../shared/dom';
import './commitDetails.scss';
import '../shared/components/codicon';
Expand Down Expand Up @@ -81,6 +83,11 @@ export class CommitDetailsApp extends App<Serialized<State>> {
}
}),
DOM.on('[data-action="commit-actions-pin"]', 'click', e => this.onTogglePin(e)),
DOM.on<WebviewPane, WebviewPaneExpandedChangeEventDetail>(
'[data-region="rich-pane"]',
'expanded-change',
e => this.onExpandedChange(e.detail),
),
];

return disposables;
Expand Down Expand Up @@ -122,6 +129,12 @@ export class CommitDetailsApp extends App<Serialized<State>> {
}
}

private onExpandedChange(e: WebviewPaneExpandedChangeEventDetail) {
this.sendCommand(PreferencesCommandType, {
autolinksExpanded: e.expanded,
});
}

private onTogglePin(e: MouseEvent) {
e.preventDefault();
this.sendCommand(PinCommitCommandType, { pin: !this.state.pinned });
Expand Down Expand Up @@ -410,21 +423,30 @@ export class CommitDetailsApp extends App<Serialized<State>> {
}

renderPullRequestAndAutolinks(state: CommitState) {
const $el = document.querySelector<HTMLElement>('[data-region="autolinks"]');
const $el = document.querySelector<WebviewPane>('[data-region="rich-pane"]');
if ($el == null) {
return;
}

const $info = document.querySelector<HTMLElement>('[data-region="rich-info"]');
$el.expanded = this.state.preferences?.autolinksExpanded ?? true;

const $info = $el.querySelector('[data-region="rich-info"]');
const $autolinks = $el.querySelector('[data-region="autolinks"]');
if (state.pullRequest != null || state.autolinkedIssues?.length) {
$el.setAttribute('aria-hidden', 'false');
$autolinks?.setAttribute('aria-hidden', 'false');
$info?.setAttribute('aria-hidden', 'true');
this.renderPullRequest(state);
this.renderIssues(state);
} else {
$el.setAttribute('aria-hidden', 'true');
$autolinks?.setAttribute('aria-hidden', 'true');
$info?.setAttribute('aria-hidden', 'false');
}

const $count = $el.querySelector('[data-region="autolink-count"]');
if ($count == null) return;

const count = (state.pullRequest != null ? 1 : 0) + (state.autolinkedIssues?.length ?? 0);
$count.innerHTML = `${count === 0 ? 'none' : count} found`;
}

renderPullRequest(state: CommitState) {
Expand Down
17 changes: 16 additions & 1 deletion src/webviews/apps/shared/components/webview-pane.ts
Expand Up @@ -2,6 +2,10 @@ import { css, html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import './codicon';

export interface WebviewPaneExpandedChangeEventDetail {
expanded: boolean;
}

@customElement('webview-pane')
export class WebviewPane extends LitElement {
static override styles = css`
Expand Down Expand Up @@ -72,7 +76,8 @@ export class WebviewPane extends LitElement {
padding-top: 0.6rem;
}
:host([collapsable]:not([expanded])) .content {
:host([collapsable]:not([expanded])) .content,
:host([collapsable][expanded='false']) .content {
display: none;
}
`;
Expand Down Expand Up @@ -114,5 +119,15 @@ export class WebviewPane extends LitElement {

private toggleExpanded() {
this.expanded = !this.expanded;

this.dispatchEvent(
new CustomEvent<WebviewPaneExpandedChangeEventDetail>('expanded-change', {
detail: {
expanded: this.expanded,
},
bubbles: true,
composed: true,
}),
);
}
}
26 changes: 24 additions & 2 deletions src/webviews/commitDetails/commitDetailsWebviewView.ts
Expand Up @@ -9,6 +9,7 @@ import type { GitFileChange } from '../../git/models/file';
import { GitFile } from '../../git/models/file';
import type { IssueOrPullRequest } from '../../git/models/issue';
import type { PullRequest } from '../../git/models/pullRequest';
import { WorkspaceStorageKeys } from '../../storage';
import { executeCommand } from '../../system/command';
import { debug } from '../../system/decorators/log';
import type { Deferrable } from '../../system/function';
Expand All @@ -24,7 +25,7 @@ import type { ViewNode } from '../../views/nodes/viewNode';
import type { IpcMessage } from '../protocol';
import { onIpc } from '../protocol';
import { WebviewViewBase } from '../webviewViewBase';
import type { CommitDetails, FileActionParams, State } from './protocol';
import type { CommitDetails, FileActionParams, SavedPreferences, State } from './protocol';
import {
AutolinkSettingsCommandType,
CommitActionsCommandType,
Expand All @@ -36,13 +37,14 @@ import {
OpenFileOnRemoteCommandType,
PickCommitCommandType,
PinCommitCommandType,
PreferencesCommandType,
SearchCommitCommandType,
} from './protocol';

interface Context {
pinned: boolean;
commit: GitCommit | undefined;

preferences: SavedPreferences | undefined;
richStateLoaded: boolean;
formattedMessage: string | undefined;
autolinkedIssues: IssueOrPullRequest[] | undefined;
Expand All @@ -66,6 +68,11 @@ export class CommitDetailsWebviewView extends WebviewViewBase<State, Serialized<
this._context = {
pinned: false,
commit: undefined,
preferences: {
autolinksExpanded: this.container.storage.getWorkspace(
WorkspaceStorageKeys.ViewsCommitDetailsAutolinksExpanded,
),
},
richStateLoaded: false,
formattedMessage: undefined,
autolinkedIssues: undefined,
Expand Down Expand Up @@ -193,6 +200,9 @@ export class CommitDetailsWebviewView extends WebviewViewBase<State, Serialized<
case PinCommitCommandType.method:
onIpc(PinCommitCommandType, e, params => this.updatePinned(params.pin ?? false, true));
break;
case PreferencesCommandType.method:
onIpc(PreferencesCommandType, e, params => this.updatePreferences(params));
break;
}
}

Expand Down Expand Up @@ -264,6 +274,7 @@ export class CommitDetailsWebviewView extends WebviewViewBase<State, Serialized<
pinned: current.pinned,
includeRichContent: current.richStateLoaded,
// commits: commitChoices,
preferences: current.preferences,
selected: details,
autolinkedIssues: current.autolinkedIssues,
pullRequest: current.pullRequest,
Expand Down Expand Up @@ -375,6 +386,17 @@ export class CommitDetailsWebviewView extends WebviewViewBase<State, Serialized<
this.updateState(immediate);
}

private updatePreferences(preferences: SavedPreferences) {
if (this._context.preferences?.autolinksExpanded === preferences.autolinksExpanded) return;

void this.container.storage.storeWorkspace(
WorkspaceStorageKeys.ViewsCommitDetailsAutolinksExpanded,
preferences.autolinksExpanded,
);

this.updatePendingContext({ preferences: { autolinksExpanded: preferences.autolinksExpanded } });
}

private updatePendingContext(context: Partial<Context>, force: boolean = false): boolean {
let changed = false;
for (const [key, value] of Object.entries(context)) {
Expand Down
10 changes: 10 additions & 0 deletions src/webviews/commitDetails/protocol.ts
Expand Up @@ -22,8 +22,13 @@ export type CommitDetails = CommitSummary & {
stats?: GitCommitStats;
};

export type SavedPreferences = {
autolinksExpanded?: boolean;
};

export type State = {
pinned: boolean;
preferences?: SavedPreferences;
// commits?: CommitSummary[];
includeRichContent?: boolean;

Expand Down Expand Up @@ -65,6 +70,11 @@ export interface PinParams {
}
export const PinCommitCommandType = new IpcCommandType<PinParams>('commit/pin');

export interface PreferenceParams {
autolinksExpanded: boolean;
}
export const PreferencesCommandType = new IpcCommandType<PreferenceParams>('commit/preferences');

// NOTIFICATIONS

export interface DidChangeStateParams {
Expand Down

0 comments on commit 7f17d81

Please sign in to comment.