Commit 015ab77f authored by Jonathan Wilkes's avatar Jonathan Wilkes
Browse files

add shortcuts file, fix pasting pd file from clipboard

parent a61dbaa6
...@@ -157,7 +157,7 @@ var canvas_events = (function() { ...@@ -157,7 +157,7 @@ var canvas_events = (function() {
two = text.charAt(1); two = text.charAt(1);
return (one === "#" && (two === "N" || two === "X")); return (one === "#" && (two === "N" || two === "X"));
}, },
grow_svg_for_element= function(elem) { grow_svg_for_element = function(elem) {
// See if an element overflows the svg bbox, and // See if an element overflows the svg bbox, and
// enlarge the svg to accommodate it. // enlarge the svg to accommodate it.
// Note: window.scrollX and window.scrollY might not work // Note: window.scrollX and window.scrollY might not work
...@@ -817,6 +817,59 @@ var canvas_events = (function() { ...@@ -817,6 +817,59 @@ var canvas_events = (function() {
close_save_dialog: function() { close_save_dialog: function() {
document.getElementById("save_before_quit").close(); document.getElementById("save_before_quit").close();
}, },
paste_from_pd_file: function(name, clipboard_data) {
var line, lines, i, pd_message;
// This lets the user copy some Pd source file from another
// application and paste the code directly into a canvas window
// (empty or otherwise). It does a quick check to make sure the OS
// clipboard is holding text that could conceivably be Pd source
// code. But that's not 100% foolproof and the engine might crash
// and burn, so be careful! :)
// We only want to check the external buffer and/or send a "paste"
// event to Pd if the canvas is in a "normal" state.
if (canvas_events.get_state() !== "normal") {
return;
}
if (!might_be_a_pd_file(clipboard_data)) {
pdgui.post("paste error: clipboard doesn't appear to contain valid Pd code");
return;
}
// clear the buffer
pdgui.pdsend(name, "copyfromexternalbuffer");
pd_message = "";
lines = clipboard_data.split("\n");
for (i = 0; i < lines.length; i++) {
line = lines[i];
// process pd_message if it ends with a semicolon that
// isn't preceded by a backslash
if (line.slice(-1) === ";" &&
(line.length < 2 || line.slice(-2, -1) !== "\\")) {
if (pd_message === "") {
pd_message = line;
} else {
pd_message = pd_message + " " + line;
}
pdgui.pdsend(name, "copyfromexternalbuffer", pd_message);
pd_message = "";
} else {
pd_message = pd_message + " " + line;
pd_message = pd_message.replace(/\n/g, "");
}
}
// This signals to the engine that we're done filling the external
// buffer, that it can do necessary checks and call some internal
// cleanup code.
pdgui.pdsend(name, "copyfromexternalbuffer");
// Send a canvas "paste" message to Pd
pdgui.pdsend(name, "paste");
// Finally, make sure to reset the buffer so that next time around
// we start from a clean slate. (Otherwise, the engine will just
// add stuff to the existing buffer contents.)
pdgui.pdsend(name, "reset_copyfromexternalbuffer");
},
init: function() { init: function() {
document.getElementById("saveDialog") document.getElementById("saveDialog")
.setAttribute("nwworkingdir", pdgui.get_pwd()); .setAttribute("nwworkingdir", pdgui.get_pwd());
...@@ -1149,60 +1202,6 @@ function instantiate_live_box() { ...@@ -1149,60 +1202,6 @@ function instantiate_live_box() {
} }
} }
function canvas_paste_from_clipboard(name, clipboard_data)
{
var line, lines, i, pd_message;
// This lets the user copy some Pd source file from another application
// and paste the code directly into a canvas window (empty or otherwise).
// It does a quick check to make sure the OS clipboard is holding text
// that could conceivably be Pd source code. But that's not 100% foolproof
// and if it's not then the engine might crash and burn, so be careful! :)
// We only want to check the external buffer and/or send a "paste" event
// to Pd if the canvas is in a "normal" state.
if (canvas_events.get_state() !== "normal") {
return;
}
if (!might_be_a_pd_file(clipboard_data)) {
pdgui.post("paste error: clipboard doesn't appear to contain valid Pd code");
return;
}
// clear the buffer
pdgui.pdsend(name, "copyfromexternalbuffer");
pd_message = "";
lines = clipboard_data.split("\n");
for (i = 0; i < lines.length; i++) {
line = lines[i];
// process pd_message if it ends with a semicolon that
// isn't preceded by a backslash
if (line.slice(-1) === ";" &&
(line.length < 2 || line.slice(-2, -1) !== "\\")) {
if (pd_message === "") {
pd_message = line;
} else {
pd_message = pd_message + " " + line;
}
pdgui.pdsend(name, "copyfromexternalbuffer", pd_message);
pd_message = "";
} else {
pd_message = pd_message + " " + line;
pd_message = pd_message.replace(/\n/g, "");
}
}
// This signals to the engine that we're done filling the external buffer,
// that it can do necessary checks and call some internal cleanup code.
pdgui.pdsend(name, "copyfromexternalbuffer");
// Send a canvas "paste" message to Pd
pdgui.pdsend(name, "paste");
// Finally, make sure to reset the buffer so that next time around we
// start from a clean slate. (Otherwise, the engine will just add stuff to
// the existing buffer contents.)
pdgui.pdsend(name, "reset_copyfromexternalbuffer");
}
// Menus for the Patch window // Menus for the Patch window
var canvas_menu = {}; var canvas_menu = {};
...@@ -1362,7 +1361,7 @@ function nw_create_patch_window_menus(gui, w, name) { ...@@ -1362,7 +1361,7 @@ function nw_create_patch_window_menus(gui, w, name) {
var clipboard = nw.Clipboard.get(); var clipboard = nw.Clipboard.get();
var text = clipboard.get('text'); var text = clipboard.get('text');
//pdgui.post("** paste from clipboard: "+text); //pdgui.post("** paste from clipboard: "+text);
canvas_paste_from_clipboard(name, text); canvas_events.paste_from_pd_file(name, text);
} }
}); });
minit(m.edit.duplicate, { minit(m.edit.duplicate, {
......
...@@ -4,6 +4,7 @@ var pdgui = require("./pdgui.js"); ...@@ -4,6 +4,7 @@ var pdgui = require("./pdgui.js");
var l = pdgui.get_local_string; // For menu names var l = pdgui.get_local_string; // For menu names
var osx_menu = null; // OSX App menu -- a single one per running instance var osx_menu = null; // OSX App menu -- a single one per running instance
var recent_files_submenu = null; var recent_files_submenu = null;
var shortcuts = require("./pd_shortcuts.js");
function create_menu(gui, type) { function create_menu(gui, type) {
// On OSX we create a menu only once, and then enable/disable menuitems // On OSX we create a menu only once, and then enable/disable menuitems
...@@ -16,7 +17,6 @@ function create_menu(gui, type) { ...@@ -16,7 +17,6 @@ function create_menu(gui, type) {
// won't let you update the keyboard shortcut binding later.) // won't let you update the keyboard shortcut binding later.)
var m = {}, var m = {},
osx = process.platform === "darwin", osx = process.platform === "darwin",
cmd_or_ctrl = osx ? "cmd" : "ctrl", // for keybindings
canvas_menu, // menu for canvas = true, menu for Pd console = false canvas_menu, // menu for canvas = true, menu for Pd console = false
window_menu, // window menu bar, or-- for OSX-- app menu bar window_menu, // window menu bar, or-- for OSX-- app menu bar
file_menu, // submenus for window menubar... file_menu, // submenus for window menubar...
...@@ -64,14 +64,14 @@ function create_menu(gui, type) { ...@@ -64,14 +64,14 @@ function create_menu(gui, type) {
m.file = {}; m.file = {};
file_menu.append(m.file.new_file = new gui.MenuItem({ file_menu.append(m.file.new_file = new gui.MenuItem({
label: l("menu.new"), label: l("menu.new"),
key: "n", key: shortcuts.menu.new.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.new.modifiers,
tooltip: l("menu.new_tt") tooltip: l("menu.new_tt")
})); }));
file_menu.append(m.file.open = new gui.MenuItem({ file_menu.append(m.file.open = new gui.MenuItem({
label: l("menu.open"), label: l("menu.open"),
key: "o", key: shortcuts.menu.open.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.open.modifiers,
tooltip: l("menu.open_tt") tooltip: l("menu.open_tt")
})); }));
file_menu.append(m.file.recent_files = new gui.MenuItem({ file_menu.append(m.file.recent_files = new gui.MenuItem({
...@@ -89,20 +89,20 @@ function create_menu(gui, type) { ...@@ -89,20 +89,20 @@ function create_menu(gui, type) {
if (canvas_menu) { if (canvas_menu) {
file_menu.append(m.file.save = new gui.MenuItem({ file_menu.append(m.file.save = new gui.MenuItem({
label: l("menu.save"), label: l("menu.save"),
key: "s", key: shortcuts.menu.save.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.save.modifiers,
tooltip: l("menu.save_tt") tooltip: l("menu.save_tt")
})); }));
file_menu.append(m.file.saveas = new gui.MenuItem({ file_menu.append(m.file.saveas = new gui.MenuItem({
label: l("menu.saveas"), label: l("menu.saveas"),
key: "s", key: shortcuts.menu.saveas.key,
modifiers: cmd_or_ctrl + "+shift", modifiers: shortcuts.menu.saveas.modifiers,
tooltip: l("menu.saveas_tt") tooltip: l("menu.saveas_tt")
})); }));
file_menu.append(m.file.print = new gui.MenuItem({ file_menu.append(m.file.print = new gui.MenuItem({
label: l("menu.print"), label: l("menu.print"),
key: "p", key: shortcuts.menu.print.key,
modifiers: cmd_or_ctrl + "+shift", modifiers: shortcuts.menu.print.modifiers,
tooltip: l("menu.print_tt") tooltip: l("menu.print_tt")
})); }));
} }
...@@ -111,8 +111,8 @@ function create_menu(gui, type) { ...@@ -111,8 +111,8 @@ function create_menu(gui, type) {
} }
file_menu.append(m.file.message = new gui.MenuItem({ file_menu.append(m.file.message = new gui.MenuItem({
label: l("menu.message"), label: l("menu.message"),
key: "m", key: shortcuts.menu.message.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.message.modifiers,
tooltip: l("menu.message_tt") tooltip: l("menu.message_tt")
})); }));
if (pdgui.k12_mode == 0) { if (pdgui.k12_mode == 0) {
...@@ -121,15 +121,15 @@ function create_menu(gui, type) { ...@@ -121,15 +121,15 @@ function create_menu(gui, type) {
if (canvas_menu) { if (canvas_menu) {
file_menu.append(m.file.close = new gui.MenuItem({ file_menu.append(m.file.close = new gui.MenuItem({
label: l("menu.close"), label: l("menu.close"),
key: "w", key: shortcuts.menu.close.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.close.modifiers,
tooltip: l("menu.close_tt") tooltip: l("menu.close_tt")
})); }));
} }
file_menu.append(m.file.quit = new gui.MenuItem({ file_menu.append(m.file.quit = new gui.MenuItem({
label: l("menu.quit"), label: l("menu.quit"),
key: "q", key: shortcuts.menu.quit.key,
modifiers: cmd_or_ctrl modifiers: shortcuts.menu.quit.modifiers
})); }));
// Edit menu // Edit menu
...@@ -150,8 +150,8 @@ function create_menu(gui, type) { ...@@ -150,8 +150,8 @@ function create_menu(gui, type) {
edit_menu.insert(m.edit.undo = new gui.MenuItem({ edit_menu.insert(m.edit.undo = new gui.MenuItem({
label: l("menu.undo"), label: l("menu.undo"),
tooltip: l("menu.undo_tt"), tooltip: l("menu.undo_tt"),
key: "z", key: shortcuts.menu.undo.key,
modifiers: cmd_or_ctrl modifiers: shortcuts.menu.undo.modifiers
}), 0); }), 0);
m.edit.redo = window_menu.items[1].submenu.items[1]; m.edit.redo = window_menu.items[1].submenu.items[1];
...@@ -163,8 +163,8 @@ function create_menu(gui, type) { ...@@ -163,8 +163,8 @@ function create_menu(gui, type) {
edit_menu.insert(m.edit.redo = new gui.MenuItem({ edit_menu.insert(m.edit.redo = new gui.MenuItem({
label: l("menu.redo"), label: l("menu.redo"),
tooltip: l("menu.redo_tt"), tooltip: l("menu.redo_tt"),
key: "z", key: shortcuts.menu.redo.key,
modifiers: "shift+" + cmd_or_ctrl modifiers: shortcuts.menu.redo.modifiers
}), 1); }), 1);
// Note: window_menu.items[1].submenu.items[2] is the separator // Note: window_menu.items[1].submenu.items[2] is the separator
...@@ -183,8 +183,8 @@ function create_menu(gui, type) { ...@@ -183,8 +183,8 @@ function create_menu(gui, type) {
edit_menu.append(m.edit.selectall = new gui.MenuItem({ edit_menu.append(m.edit.selectall = new gui.MenuItem({
label: l("menu.selectall"), label: l("menu.selectall"),
tooltip: l("menu.selectall_tt"), tooltip: l("menu.selectall_tt"),
key: "a", key: shortcuts.menu.selectall.key,
modifiers: cmd_or_ctrl modifiers: shortcuts.menu.selectall.modifiers
})); }));
// Finally, let's remove the "Delete" item since it's not hooked // Finally, let's remove the "Delete" item since it's not hooked
// in to anything yet... // in to anything yet...
...@@ -196,34 +196,34 @@ function create_menu(gui, type) { ...@@ -196,34 +196,34 @@ function create_menu(gui, type) {
edit_menu.append(m.edit.undo = new gui.MenuItem({ edit_menu.append(m.edit.undo = new gui.MenuItem({
label: l("menu.undo"), label: l("menu.undo"),
tooltip: l("menu.undo_tt"), tooltip: l("menu.undo_tt"),
key: "z", key: shortcuts.menu.undo.key,
modifiers: cmd_or_ctrl modifiers: shortcuts.menu.undo.modifiers
})); }));
edit_menu.append(m.edit.redo = new gui.MenuItem({ edit_menu.append(m.edit.redo = new gui.MenuItem({
label: l("menu.redo"), label: l("menu.redo"),
tooltip: l("menu.redo_tt"), tooltip: l("menu.redo_tt"),
key: "z", key: shortcuts.menu.redo.key,
modifiers: "shift+" + cmd_or_ctrl modifiers: shortcuts.menu.redo.modifiers
})); }));
edit_menu.append(new gui.MenuItem({ type: "separator" })); edit_menu.append(new gui.MenuItem({ type: "separator" }));
edit_menu.append(m.edit.cut = new gui.MenuItem({ edit_menu.append(m.edit.cut = new gui.MenuItem({
label: l("menu.cut"), label: l("menu.cut"),
key: "x", key: shortcuts.menu.cut.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.cut.modifiers,
tooltip: l("menu.cut_tt") tooltip: l("menu.cut_tt")
})); }));
} }
edit_menu.append(m.edit.copy = new gui.MenuItem({ edit_menu.append(m.edit.copy = new gui.MenuItem({
label: l("menu.copy"), label: l("menu.copy"),
key: "c", key: shortcuts.menu.copy.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.copy.modifiers,
tooltip: l("menu.copy_tt") tooltip: l("menu.copy_tt")
})); }));
if (canvas_menu) { if (canvas_menu) {
edit_menu.append(m.edit.paste = new gui.MenuItem({ edit_menu.append(m.edit.paste = new gui.MenuItem({
label: l("menu.paste"), label: l("menu.paste"),
key: "v", key: shortcuts.menu.paste.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.paste.modifiers,
tooltip: l("menu.paste_tt") tooltip: l("menu.paste_tt")
})); }));
} }
...@@ -235,14 +235,14 @@ function create_menu(gui, type) { ...@@ -235,14 +235,14 @@ function create_menu(gui, type) {
if (canvas_menu) { if (canvas_menu) {
edit_menu.append(m.edit.paste_clipboard = new gui.MenuItem({ edit_menu.append(m.edit.paste_clipboard = new gui.MenuItem({
label: l("menu.paste_clipboard"), label: l("menu.paste_clipboard"),
key: "v", key: shortcuts.menu.paste_clipboard.key,
modifiers: cmd_or_ctrl + "+alt", modifiers: shortcuts.menu.paste_clipboard.modifiers,
tooltip: l("menu.paste_clipboard_tt") tooltip: l("menu.paste_clipboard_tt")
})); }));
edit_menu.append(m.edit.duplicate = new gui.MenuItem({ edit_menu.append(m.edit.duplicate = new gui.MenuItem({
label: l("menu.duplicate"), label: l("menu.duplicate"),
key: "d", key: shortcuts.menu.duplicate.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.duplicate.modifiers,
tooltip: l("menu.duplicate_tt") tooltip: l("menu.duplicate_tt")
})); }));
} }
...@@ -251,8 +251,8 @@ function create_menu(gui, type) { ...@@ -251,8 +251,8 @@ function create_menu(gui, type) {
if (!osx) { if (!osx) {
edit_menu.append(m.edit.selectall = new gui.MenuItem({ edit_menu.append(m.edit.selectall = new gui.MenuItem({
label: l("menu.selectall"), label: l("menu.selectall"),
key: "a", key: shortcuts.menu.selectall.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.selectall.modifiers,
tooltip: l("menu.selectall_tt") tooltip: l("menu.selectall_tt")
})); }));
} }
...@@ -264,8 +264,8 @@ function create_menu(gui, type) { ...@@ -264,8 +264,8 @@ function create_menu(gui, type) {
// tried fromCharCode...) // tried fromCharCode...)
edit_menu.append(m.edit.reselect = new gui.MenuItem({ edit_menu.append(m.edit.reselect = new gui.MenuItem({
label: l("menu.reselect"), label: l("menu.reselect"),
key: String.fromCharCode(10), key: shortcuts.menu.reselect.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.reselect.modifiers,
tooltip: l("menu.reselect_tt") tooltip: l("menu.reselect_tt")
})); }));
} }
...@@ -273,15 +273,15 @@ function create_menu(gui, type) { ...@@ -273,15 +273,15 @@ function create_menu(gui, type) {
edit_menu.append(m.edit.clear_console = new gui.MenuItem({ edit_menu.append(m.edit.clear_console = new gui.MenuItem({
label: l("menu.clear_console"), label: l("menu.clear_console"),
tooltip: l("menu.clear_console"), tooltip: l("menu.clear_console"),
key: "l", key: shortcuts.menu.clear_console.key,
modifiers: "shift+" + cmd_or_ctrl modifiers: shortcuts.menu.clear_console.modifiers
})); }));
edit_menu.append(new gui.MenuItem({ type: "separator" })); edit_menu.append(new gui.MenuItem({ type: "separator" }));
if (canvas_menu) { if (canvas_menu) {
edit_menu.append(m.edit.tidyup = new gui.MenuItem({ edit_menu.append(m.edit.tidyup = new gui.MenuItem({
label: l("menu.tidyup"), label: l("menu.tidyup"),
key: "y", key: shortcuts.menu.tidyup.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.tidyup.modifiers,
tooltip: l("menu.tidyup_tt") tooltip: l("menu.tidyup_tt")
})); }));
edit_menu.append(m.edit.font = new gui.MenuItem({ edit_menu.append(m.edit.font = new gui.MenuItem({
...@@ -291,23 +291,23 @@ function create_menu(gui, type) { ...@@ -291,23 +291,23 @@ function create_menu(gui, type) {
edit_menu.append(m.edit.cordinspector = new gui.MenuItem({ edit_menu.append(m.edit.cordinspector = new gui.MenuItem({
type: "checkbox", type: "checkbox",
label: l("menu.cordinspector"), label: l("menu.cordinspector"),
key: "r", key: shortcuts.menu.cordinspector.key,
modifiers: cmd_or_ctrl + "+shift", modifiers: shortcuts.menu.cordinspector.modifiers,
tooltip: l("menu.cordinspector_tt") tooltip: l("menu.cordinspector_tt")
})); }));
edit_menu.append(new gui.MenuItem({ type: "separator" })); edit_menu.append(new gui.MenuItem({ type: "separator" }));
} }
edit_menu.append(m.edit.find = new gui.MenuItem({ edit_menu.append(m.edit.find = new gui.MenuItem({
label: l("menu.find"), label: l("menu.find"),
key: "f", key: shortcuts.menu.find.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.find.modifiers,
tooltip: l("menu.find_tt") tooltip: l("menu.find_tt")
})); }));
if (canvas_menu) { if (canvas_menu) {
edit_menu.append(m.edit.findagain = new gui.MenuItem({ edit_menu.append(m.edit.findagain = new gui.MenuItem({
label: l("menu.findagain"), label: l("menu.findagain"),
key: "g", key: shortcuts.menu.findagain.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.findagain.modifiers,
tooltip: l("menu.findagain") tooltip: l("menu.findagain")
})); }));
edit_menu.append(m.edit.finderror = new gui.MenuItem({ edit_menu.append(m.edit.finderror = new gui.MenuItem({
...@@ -324,16 +324,16 @@ function create_menu(gui, type) { ...@@ -324,16 +324,16 @@ function create_menu(gui, type) {
edit_menu.append(m.edit.editmode = new gui.MenuItem({ edit_menu.append(m.edit.editmode = new gui.MenuItem({
type: "checkbox", type: "checkbox",
label: l("menu.editmode"), label: l("menu.editmode"),
key: "e", key: shortcuts.menu.editmode.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.editmode.modifiers,
tooltip: l("menu.editmode_tt") tooltip: l("menu.editmode_tt")
})); }));
edit_menu.append(new gui.MenuItem({ type: "separator" })); edit_menu.append(new gui.MenuItem({ type: "separator" }));
} }
edit_menu.append(m.edit.preferences = new gui.MenuItem({ edit_menu.append(m.edit.preferences = new gui.MenuItem({
label: l("menu.preferences"), label: l("menu.preferences"),
key: osx ? "," : "p", key: shortcuts.menu.preferences.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.preferences.modifiers,
tooltip: l("menu.preferences_tt") tooltip: l("menu.preferences_tt")
})); }));
...@@ -344,48 +344,48 @@ function create_menu(gui, type) { ...@@ -344,48 +344,48 @@ function create_menu(gui, type) {
m.view = {}; m.view = {};
view_menu.append(m.view.zoomin = new gui.MenuItem({ view_menu.append(m.view.zoomin = new gui.MenuItem({
label: l("menu.zoomin"), label: l("menu.zoomin"),
key: "=", key: shortcuts.menu.zoomin.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.zoomin.modifiers,
tooltip: l("menu.zoomin_tt") tooltip: l("menu.zoomin_tt")
})); }));
view_menu.append(m.view.zoomout = new gui.MenuItem({ view_menu.append(m.view.zoomout = new gui.MenuItem({
label: l("menu.zoomout"), label: l("menu.zoomout"),
key: "-", key: shortcuts.menu.zoomout.key,
modifiers: cmd_or_ctrl, modifiers: shortcuts.menu.zoomout.modifiers,
tooltip: l("menu.zoomout_tt") tooltip: l("menu.zoomout_tt")
})); }));
view_menu.append(new gui.MenuItem({ type: "separator" })); view_menu.append(new gui.MenuItem({ type: "separator" }));
view_menu.append(m.view.zoomreset = new gui.MenuItem({