|
| 1 | +/** @babel */ |
| 2 | + |
| 3 | +import fs from 'fs' |
| 4 | +import path from 'path' |
| 5 | +import { CompositeDisposable, Disposable } from 'atom' |
| 6 | +import { client } from '../connection' |
| 7 | +import { show } from '../ui/selector' |
| 8 | + |
| 9 | +const { allProjects, activateProject } = client.import({ rpc: ['allProjects'], msg: ['activateProject'] }) |
| 10 | + |
| 11 | +let ink |
| 12 | +export function consumeInk (_ink) { |
| 13 | + ink = _ink |
| 14 | +} |
| 15 | + |
| 16 | +export function consumeStatusBar (statusBar) { |
| 17 | + const subs = new CompositeDisposable() |
| 18 | + |
| 19 | + const dom = document.createElement('a') |
| 20 | + const tileDom = document.createElement('span') // only `span` element can be hide completely |
| 21 | + tileDom.classList.add('julia', 'inline-block') |
| 22 | + tileDom.appendChild(dom) |
| 23 | + const tile = statusBar.addRightTile({ |
| 24 | + item: tileDom, |
| 25 | + priority: 10 |
| 26 | + }) |
| 27 | + |
| 28 | + let projectName = '' |
| 29 | + let projectPath = '' |
| 30 | + |
| 31 | + const showTile = () => tileDom.style.display = '' |
| 32 | + const hideTile = () => tileDom.style.display = 'none' |
| 33 | + const updateTile = (proj) => { |
| 34 | + if (!proj) return hideTile() |
| 35 | + projectName = proj.name |
| 36 | + dom.innerText = 'Env: ' + projectName |
| 37 | + projectPath = proj.path |
| 38 | + showTile() |
| 39 | + } |
| 40 | + client.handle({ updateProject: updateTile }) |
| 41 | + |
| 42 | + const onClick = (event) => { |
| 43 | + if (process.platform === 'darwin' ? event.metaKey : event.ctrlKey) { |
| 44 | + if (!fs.existsSync(projectPath)) return |
| 45 | + const pending = atom.config.get('core.allowPendingPaneItems') |
| 46 | + if (ink) { |
| 47 | + ink.Opener.open(projectPath, { |
| 48 | + pending, |
| 49 | + }) |
| 50 | + } else { |
| 51 | + atom.workspace.open(projectPath, { |
| 52 | + pending, |
| 53 | + searchAllPanes: true |
| 54 | + }) |
| 55 | + } |
| 56 | + } else { |
| 57 | + chooseEnvironment() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const modifiler = process.platform == 'darwin' ? 'Cmd' : 'Ctrl' |
| 62 | + const title = () => { |
| 63 | + return `Currently working in environment ${projectName} at ${projectPath}<br >` + |
| 64 | + `Click to choose an environment<br >` + |
| 65 | + `${modifiler}-Click to open project file` |
| 66 | + } |
| 67 | + |
| 68 | + dom.addEventListener('click', onClick) |
| 69 | + subs.add( |
| 70 | + client.onDetached(hideTile), |
| 71 | + atom.tooltips.add(dom, { title }), |
| 72 | + new Disposable(() => { |
| 73 | + dom.removeEventListener('click', onClick) |
| 74 | + tile.destroy() |
| 75 | + }) |
| 76 | + ) |
| 77 | + |
| 78 | + hideTile() |
| 79 | + return subs |
| 80 | +} |
| 81 | + |
| 82 | +export function chooseEnvironment () { |
| 83 | + client.require('choose environment', () => { |
| 84 | + allProjects() |
| 85 | + .then(({ projects, active }) => { |
| 86 | + if (!projects) throw '`allProject` handler unsupported' |
| 87 | + if (projects.length === 0) throw 'no environment found' |
| 88 | + projects = projects.map(proj => { |
| 89 | + proj.primary = proj.name |
| 90 | + proj.secondary = proj.path |
| 91 | + return proj |
| 92 | + }) |
| 93 | + return { projects, active } |
| 94 | + }) |
| 95 | + .then(({ projects, active }) => { |
| 96 | + show(projects, { active }).then(proj => { |
| 97 | + if (!proj) return |
| 98 | + const dir = path.dirname(proj.path) |
| 99 | + activateProject(dir) |
| 100 | + }) |
| 101 | + }) |
| 102 | + .catch(err => console.log(err)) |
| 103 | + }) |
| 104 | +} |
0 commit comments