|
| 1 | +// import { OpenerService } from '@theia/core/lib/browser'; |
| 2 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 3 | +import { /*inject,*/ injectable } from '@theia/core/shared/inversify'; |
| 4 | +import React from '@theia/core/shared/react'; |
| 5 | +import { TreeViewWidget as TheiaTreeViewWidget } from '@theia/plugin-ext/lib/main/browser/view/tree-view-widget'; |
| 6 | + |
| 7 | +@injectable() |
| 8 | +export class TreeViewWidget extends TheiaTreeViewWidget { |
| 9 | + // @inject(OpenerService) |
| 10 | + // private readonly openerService: OpenerService; |
| 11 | + private readonly toDisposeBeforeUpdateViewWelcomeNodes = |
| 12 | + new DisposableCollection(); |
| 13 | + |
| 14 | + // The actual rewrite of the viewsWelcome rendering aligned to VS Code to fix https://github.com/eclipse-theia/theia/issues/14309 |
| 15 | + // Based on https://github.com/microsoft/vscode/blob/56b535f40900080fac8202c77914c5ce49fa4aae/src/vs/workbench/browser/parts/views/viewPane.ts#L228-L299 |
| 16 | + protected override updateViewWelcomeNodes(): void { |
| 17 | + this.toDisposeBeforeUpdateViewWelcomeNodes.dispose(); |
| 18 | + const viewWelcomes = this.visibleItems.sort((a, b) => a.order - b.order); |
| 19 | + this.viewWelcomeNodes = []; |
| 20 | + const allEnablementKeys: Set<string>[] = []; |
| 21 | + // the plugin-view-registry will push the changes when there is a change in the when context |
| 22 | + // this listener is to update the view when the `enablement` of the viewWelcomes changes |
| 23 | + this.toDisposeBeforeUpdateViewWelcomeNodes.push( |
| 24 | + this.contextKeyService.onDidChange((event) => { |
| 25 | + if (allEnablementKeys.some((keys) => event.affects(keys))) { |
| 26 | + this.updateViewWelcomeNodes(); |
| 27 | + this.update(); |
| 28 | + } |
| 29 | + }) |
| 30 | + ); |
| 31 | + // TODO: support `renderSecondaryButtons` prop from VS Code? |
| 32 | + for (const viewWelcome of viewWelcomes) { |
| 33 | + const { content } = viewWelcome; |
| 34 | + const enablement = isEnablementAware(viewWelcome) |
| 35 | + ? viewWelcome.enablement |
| 36 | + : undefined; |
| 37 | + const enablementKeys = enablement |
| 38 | + ? this.contextKeyService.parseKeys(enablement) |
| 39 | + : undefined; |
| 40 | + if (enablementKeys) { |
| 41 | + allEnablementKeys.push(enablementKeys); |
| 42 | + } |
| 43 | + const lines = content.split('\n'); |
| 44 | + |
| 45 | + for (let line of lines) { |
| 46 | + line = line.trim(); |
| 47 | + |
| 48 | + if (!line) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + const linkedText = parseLinkedText(line); |
| 53 | + |
| 54 | + if ( |
| 55 | + linkedText.nodes.length === 1 && |
| 56 | + typeof linkedText.nodes[0] !== 'string' |
| 57 | + ) { |
| 58 | + const node = linkedText.nodes[0]; |
| 59 | + this.viewWelcomeNodes.push( |
| 60 | + this.renderButtonNode( |
| 61 | + node, |
| 62 | + this.viewWelcomeNodes.length, |
| 63 | + enablement |
| 64 | + ) |
| 65 | + ); |
| 66 | + } else { |
| 67 | + const paragraphNodes: React.ReactNode[] = []; |
| 68 | + for (const node of linkedText.nodes) { |
| 69 | + if (typeof node === 'string') { |
| 70 | + paragraphNodes.push( |
| 71 | + this.renderTextNode(node, this.viewWelcomeNodes.length) |
| 72 | + ); |
| 73 | + } else { |
| 74 | + paragraphNodes.push( |
| 75 | + this.renderCommandLinkNode( |
| 76 | + node, |
| 77 | + this.viewWelcomeNodes.length, |
| 78 | + enablement |
| 79 | + ) |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + if (paragraphNodes.length) { |
| 84 | + this.viewWelcomeNodes.push( |
| 85 | + <p key={`p-${this.viewWelcomeNodes.length}`}> |
| 86 | + {...paragraphNodes} |
| 87 | + </p> |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected override renderButtonNode( |
| 96 | + node: ILink, |
| 97 | + lineKey: string | number, |
| 98 | + enablement: string | undefined = undefined |
| 99 | + ): React.ReactNode { |
| 100 | + return ( |
| 101 | + <div key={`line-${lineKey}`} className="theia-WelcomeViewButtonWrapper"> |
| 102 | + <button |
| 103 | + title={node.title} |
| 104 | + className="theia-button theia-WelcomeViewButton" |
| 105 | + disabled={!this.isEnabled(enablement)} |
| 106 | + onClick={(e) => this.open(e, node)} |
| 107 | + > |
| 108 | + {node.label} |
| 109 | + </button> |
| 110 | + </div> |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + protected override renderCommandLinkNode( |
| 115 | + node: ILink, |
| 116 | + linkKey: string | number, |
| 117 | + enablement: string | undefined = undefined |
| 118 | + ): React.ReactNode { |
| 119 | + return ( |
| 120 | + <a |
| 121 | + key={`link-${linkKey}`} |
| 122 | + className={this.getLinkClassName(node.href, enablement)} |
| 123 | + title={node.title ?? ''} |
| 124 | + onClick={(e) => this.open(e, node)} |
| 125 | + > |
| 126 | + {node.label} |
| 127 | + </a> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + protected override renderTextNode( |
| 132 | + node: string, |
| 133 | + textKey: string | number |
| 134 | + ): React.ReactNode { |
| 135 | + return <span key={`text-${textKey}`}>{node}</span>; |
| 136 | + } |
| 137 | + |
| 138 | + protected override getLinkClassName( |
| 139 | + href: string, |
| 140 | + enablement: string | undefined = undefined |
| 141 | + ): string { |
| 142 | + const classNames = ['theia-WelcomeViewCommandLink']; |
| 143 | + // Only command-backed links can be disabled. All other, https:, file: remain enabled |
| 144 | + if (href.startsWith('command:') && !this.isEnabled(enablement)) { |
| 145 | + classNames.push('disabled'); |
| 146 | + } |
| 147 | + return classNames.join(' '); |
| 148 | + } |
| 149 | + |
| 150 | + private open(event: React.MouseEvent, node: ILink): void { |
| 151 | + event.preventDefault(); |
| 152 | + if (node.href.startsWith('command:')) { |
| 153 | + const commandId = node.href.substring('commands:'.length - 1); |
| 154 | + this.commands.executeCommand(commandId); |
| 155 | + } else if (node.href.startsWith('file:')) { |
| 156 | + // TODO: check what Code does |
| 157 | + } else if (node.href.startsWith('https:')) { |
| 158 | + this.windowService.openNewWindow(node.href, { external: true }); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param enablement [when context](https://code.visualstudio.com/api/references/when-clause-contexts) expression string |
| 164 | + */ |
| 165 | + private isEnabled(enablement: string | undefined): boolean { |
| 166 | + return typeof enablement === 'string' |
| 167 | + ? this.contextKeyService.match(enablement) |
| 168 | + : true; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +interface EnablementAware { |
| 173 | + readonly enablement: string | undefined; |
| 174 | +} |
| 175 | + |
| 176 | +function isEnablementAware(arg: unknown): arg is EnablementAware { |
| 177 | + return !!arg && typeof arg === 'object' && 'enablement' in arg; |
| 178 | +} |
| 179 | + |
| 180 | +// https://github.com/microsoft/vscode/blob/56b535f40900080fac8202c77914c5ce49fa4aae/src/vs/base/common/linkedText.ts#L8-L56 |
| 181 | +export interface ILink { |
| 182 | + readonly label: string; |
| 183 | + readonly href: string; |
| 184 | + readonly title?: string; |
| 185 | +} |
| 186 | + |
| 187 | +export type LinkedTextNode = string | ILink; |
| 188 | + |
| 189 | +export class LinkedText { |
| 190 | + constructor(readonly nodes: LinkedTextNode[]) {} |
| 191 | + toString(): string { |
| 192 | + return this.nodes |
| 193 | + .map((node) => (typeof node === 'string' ? node : node.label)) |
| 194 | + .join(''); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +const LINK_REGEX = |
| 199 | + /\[([^\]]+)\]\(((?:https?:\/\/|command:|file:)[^\)\s]+)(?: (["'])(.+?)(\3))?\)/gi; |
| 200 | + |
| 201 | +export function parseLinkedText(text: string): LinkedText { |
| 202 | + const result: LinkedTextNode[] = []; |
| 203 | + |
| 204 | + let index = 0; |
| 205 | + let match: RegExpExecArray | null; |
| 206 | + |
| 207 | + while ((match = LINK_REGEX.exec(text))) { |
| 208 | + if (match.index - index > 0) { |
| 209 | + result.push(text.substring(index, match.index)); |
| 210 | + } |
| 211 | + |
| 212 | + const [, label, href, , title] = match; |
| 213 | + |
| 214 | + if (title) { |
| 215 | + result.push({ label, href, title }); |
| 216 | + } else { |
| 217 | + result.push({ label, href }); |
| 218 | + } |
| 219 | + |
| 220 | + index = match.index + match[0].length; |
| 221 | + } |
| 222 | + |
| 223 | + if (index < text.length) { |
| 224 | + result.push(text.substring(index)); |
| 225 | + } |
| 226 | + |
| 227 | + return new LinkedText(result); |
| 228 | +} |
0 commit comments