-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdialogs.js
More file actions
89 lines (81 loc) · 3.7 KB
/
dialogs.js
File metadata and controls
89 lines (81 loc) · 3.7 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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets */
define(function (require, exports, module) {
"use strict";
var Dialogs = brackets.getModule("widgets/Dialogs"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
Mustache = brackets.getModule("thirdparty/mustache/mustache");
var Strings = require("strings"),
_pathDialogTemplate = require("text!templates/path-dialog.html");
function getRelativeFilename(basePath, filename) {
if (basePath.endsWith('/')) {
basePath = basePath.slice(0, basePath.length - 1);
}
var basename = filename.slice(filename.lastIndexOf('/') + 1);
var dirname = filename.slice(0, filename.lastIndexOf('/') + 1);
var relapath = "";
while (!dirname.startsWith(basePath)) {
relapath = relapath + "../";
basePath = basePath.slice(0, basePath.lastIndexOf('/'));
}
return relapath + dirname.slice(basePath.length + 1) + basename;
}
function getRelativeFile(basePath, cb) {
FileSystem.showOpenDialog(false, false, "Select file", null, null, function (err, files) {
if (files && files[0]) {
cb(getRelativeFilename(basePath, files[0]));
} else {
cb("");
}
});
}
function displayPathDialog(editor, templateVars, fn) {
var selection = editor.getSelection();
templateVars.textInit = editor.document.getRange(selection.start, selection.end);
templateVars.pathInit = (templateVars.textInit.includes("://")) ? templateVars.textInit : "";
var dialog = Dialogs.showModalDialogUsingTemplate(Mustache.render(_pathDialogTemplate, templateVars));
var textField = dialog.getElement().find(".input-text");
var pathField = dialog.getElement().find(".input-path");
var fileButton = dialog.getElement().find("#choose-file");
fileButton.on("click", function () {
getRelativeFile(editor.getFile().parentPath, function (filename) {
pathField.val(filename);
});
});
dialog.done(function (buttonId) {
if (buttonId === Dialogs.DIALOG_BTN_OK) {
fn(textField.val(), pathField.val());
}
});
}
exports.image = function (editor) {
var templateVars = {
Strings: Strings,
dialogTitle: Strings.IMAGE_DIALOG,
textTitle: Strings.IMAGE_TEXT_TITLE,
textPlaceholder: Strings.IMAGE_TEXT_PLACEHOLDER,
pathTitle: Strings.IMAGE_PATH_TITLE,
pathPlaceholder: Strings.IMAGE_PATH_PLACEHOLDER
};
var selection = editor.getSelection();
displayPathDialog(editor, templateVars, function (textField, pathField) {
var imageString = "";
editor.document.replaceRange(imageString, selection.start, selection.end, "+mdbar");
});
};
exports.link = function (editor) {
var templateVars = {
Strings: Strings,
dialogTitle: Strings.LINK_DIALOG,
textTitle: Strings.LINK_TEXT_TITLE,
textPlaceholder: Strings.LINK_TEXT_PLACEHOLDER,
pathTitle: Strings.LINK_PATH_TITLE,
pathPlaceholder: Strings.LINK_PATH_PLACEHOLDER
};
var selection = editor.getSelection();
displayPathDialog(editor, templateVars, function (textField, pathField) {
var linkString = "[" + textField + "](" + pathField + ")";
editor.document.replaceRange(linkString, selection.start, selection.end, "+mdbar");
});
};
});