-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathmain.js
More file actions
125 lines (110 loc) · 5.13 KB
/
Copy pathmain.js
File metadata and controls
125 lines (110 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2018 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
define(function (require, exports, module) {
var AppInit = require("utils/AppInit"),
QuickOpen = require("search/QuickOpen"),
PathUtils = require("thirdparty/path-utils/path-utils"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
MainViewManager = require("view/MainViewManager"),
Menus = require("command/Menus");
const HTTP_PROTOCOL = "http:",
HTTPS_PROTOCOL = "https:",
TAURI_PROTOCOL = "phtauri:",
TAURI_ASSET_PROTOCOL = "asset:";
/**
* Disable context menus which are not useful for remote file
*/
function _setMenuItemsVisible() {
var file = MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE),
cMenuItems = [Commands.FILE_SAVE, Commands.FILE_RENAME, Commands.NAVIGATE_SHOW_IN_FILE_TREE],
// Enable menu options when no file is present in active pane
enable = !file || (file.constructor.name !== "RemoteFile");
// Enable or disable commands based on whether the file is a remoteFile or not.
cMenuItems.forEach(function (item) {
CommandManager.get(item).setEnabled(enable);
});
}
function _getGitHubRawURL(urlObject) {
let pathVector = urlObject.pathname.split("/");
let BLOB_STRING_LOCATION = 3;
if(pathVector.length>BLOB_STRING_LOCATION+1 && pathVector[BLOB_STRING_LOCATION] === "blob"){
// Github blob URL of the form https://github.com/brackets-cont/brackets/blob/master/.gitignore
// transform to https://raw.githubusercontent.com/brackets-cont/brackets/master/.gitignore
pathVector.splice(BLOB_STRING_LOCATION,1);
let newPath = pathVector.join("/");
return `https://raw.githubusercontent.com${newPath}`;
}
return urlObject.href;
}
function _getGitLabRawURL(urlObject) {
// Gitlab does not specify CORS, so this wont work in phoenix, but will work in brackets for now
let pathVector = urlObject.pathname.split("/");
let BLOB_STRING_LOCATION = 4;
if(pathVector.length>BLOB_STRING_LOCATION+1 && pathVector[BLOB_STRING_LOCATION] === "blob"){
// GitLab blob URL of the form https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/.codeclimate.yml
// transform to https://gitlab.com/gitlab-org/gitlab-foss/-/raw/master/.codeclimate.yml
pathVector[BLOB_STRING_LOCATION] = "raw";
let newPath = pathVector.join("/");
return `https://gitlab.com${newPath}`;
}
return urlObject.href;
}
/**
* Checks the URL to see if it is from known code URL sites(Eg. Github) and transforms
* it into URLs to fetch raw code.
* @param url
* @private
* @return code URL if transformed, else returns the arg URL as is
*/
function _getRawURL(url) {
let urlObject = new URL(url);
switch (urlObject.hostname) {
case "github.com": return _getGitHubRawURL(urlObject);
case "gitlab.com": return _getGitLabRawURL(urlObject);
default: return url;
}
}
AppInit.htmlReady(function () {
Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_CONTEXT_MENU).on("beforeContextMenuOpen", _setMenuItemsVisible);
MainViewManager.on("currentFileChange", _setMenuItemsVisible);
// Register as quick open plugin for file URI's having HTTP:|HTTPS: protocol
QuickOpen.addQuickOpenPlugin(
{
name: "Remote file URI input",
languageIds: [], // for all language modes
search: function () {
return $.Deferred().resolve([arguments[0]]);
},
match: function (query) {
var protocol = PathUtils.parseUrl(query).protocol;
return [HTTP_PROTOCOL, HTTPS_PROTOCOL, TAURI_PROTOCOL, TAURI_ASSET_PROTOCOL].indexOf(protocol) !== -1;
},
itemFocus: function (query) {
// no op
},
itemSelect: function () {
CommandManager.execute(Commands.FILE_OPEN, {fullPath: _getRawURL(arguments[0])});
}
}
);
});
});