|
| 1 | +// eslint-disable-next-line no-use-before-define |
| 2 | +import React from 'react' |
| 3 | +import ReactDOM from 'react-dom' |
| 4 | +import Registry from '../state/registry' |
| 5 | +import packageJson from '../../../../../package.json' |
| 6 | +import { Plugin } from '@remixproject/engine' |
| 7 | +import { EventEmitter } from 'events' |
| 8 | +import { IconRecord, RemixUiVerticalIconsPanel } from '@remix-ui/vertical-icons-panel' |
| 9 | +import { Profile } from '@remixproject/plugin-utils' |
| 10 | +import { timeStamp } from 'console' |
| 11 | + |
| 12 | +const profile = { |
| 13 | + name: 'menuicons', |
| 14 | + displayName: 'Vertical Icons', |
| 15 | + description: '', |
| 16 | + version: packageJson.version, |
| 17 | + methods: ['select', 'unlinkContent', 'linkContent'], |
| 18 | + events: ['toggleContent', 'showContent'] |
| 19 | +} |
| 20 | + |
| 21 | +export class VerticalIcons extends Plugin { |
| 22 | + events: EventEmitter |
| 23 | + htmlElement: HTMLDivElement |
| 24 | + icons: Record<string, IconRecord> = {} |
| 25 | + constructor () { |
| 26 | + super(profile) |
| 27 | + this.events = new EventEmitter() |
| 28 | + this.htmlElement = document.createElement('div') |
| 29 | + this.htmlElement.setAttribute('id', 'icon-panel') |
| 30 | + } |
| 31 | + |
| 32 | + renderComponent () { |
| 33 | + const fixedOrder = ['filePanel', 'solidity','udapp', 'debugger', 'solidityStaticAnalysis', 'solidityUnitTesting', 'pluginManager'] |
| 34 | + |
| 35 | + const divived = Object.values(this.icons).map((value) => { return { |
| 36 | + ...value, |
| 37 | + isRequired: fixedOrder.indexOf(value.profile.name) > -1 |
| 38 | + }}).sort((a,b) => { |
| 39 | + return a.timestamp - b.timestamp |
| 40 | + }) |
| 41 | + |
| 42 | + const required = divived.filter((value) => value.isRequired).sort((a,b) => { |
| 43 | + return fixedOrder.indexOf(a.profile.name) - fixedOrder.indexOf(b.profile.name) |
| 44 | + }) |
| 45 | + |
| 46 | + const sorted: IconRecord[] = [ |
| 47 | + ...required, |
| 48 | + ...divived.filter((value) => { return !value.isRequired }) |
| 49 | + ] |
| 50 | + |
| 51 | + ReactDOM.render( |
| 52 | + <RemixUiVerticalIconsPanel |
| 53 | + verticalIconsPlugin={this} |
| 54 | + icons={sorted} |
| 55 | + />, |
| 56 | + this.htmlElement) |
| 57 | + } |
| 58 | + |
| 59 | + onActivation () { |
| 60 | + this.renderComponent() |
| 61 | + this.on('sidePanel', 'focusChanged', (name: string) => { |
| 62 | + Object.keys(this.icons).map((o) => { |
| 63 | + this.icons[o].active = false |
| 64 | + }) |
| 65 | + this.icons[name].active = true |
| 66 | + this.renderComponent() |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + async linkContent (profile: Profile) { |
| 71 | + if (!profile.icon) return |
| 72 | + if (!profile.kind) profile.kind = 'none' |
| 73 | + this.icons[profile.name] = { |
| 74 | + profile: profile, |
| 75 | + active: false, |
| 76 | + canbeDeactivated: await this.call('manager', 'canDeactivate', this.profile, profile), |
| 77 | + timestamp: Date.now() |
| 78 | + } |
| 79 | + this.renderComponent() |
| 80 | + } |
| 81 | + |
| 82 | + unlinkContent (profile: Profile) { |
| 83 | + delete this.icons[profile.name] |
| 84 | + this.renderComponent() |
| 85 | + } |
| 86 | + |
| 87 | + async activateHome() { |
| 88 | + await this.call('manager', 'activatePlugin', 'home') |
| 89 | + await this.call('tabs', 'focus', 'home') |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Set an icon as active |
| 94 | + * @param {string} name Name of profile of the module to activate |
| 95 | + */ |
| 96 | + select (name: string) { |
| 97 | + // TODO: Only keep `this.emit` (issue#2210) |
| 98 | + console.log(name, this) |
| 99 | + this.emit('showContent', name) |
| 100 | + this.events.emit('showContent', name) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Toggles the side panel for plugin |
| 105 | + * @param {string} name Name of profile of the module to activate |
| 106 | + */ |
| 107 | + toggle (name: string) { |
| 108 | + // TODO: Only keep `this.emit` (issue#2210) |
| 109 | + this.emit('toggleContent', name) |
| 110 | + this.events.emit('toggleContent', name) |
| 111 | + } |
| 112 | + |
| 113 | + render () { |
| 114 | + return this.htmlElement |
| 115 | + } |
| 116 | +} |
0 commit comments