styleeditor-commands.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* globals gDevTools */
  5. "use strict";
  6. const l10n = require("gcli/l10n");
  7. loader.lazyRequireGetter(this, "gDevTools",
  8. "devtools/client/framework/devtools", true);
  9. /**
  10. * The `edit` command opens the toolbox to the style editor, with a given
  11. * stylesheet open.
  12. *
  13. * This command is tricky. The 'edit' command uses the toolbox, so it's
  14. * clearly runAt:client, but it uses the 'resource' type which accesses the
  15. * DOM, so it must also be runAt:server.
  16. *
  17. * Our solution is to have the command technically be runAt:server, but to not
  18. * actually do anything other than basically `return args;`, and have the
  19. * converter (all converters are runAt:client) do the actual work of opening
  20. * a toolbox.
  21. *
  22. * For alternative solutions that we considered, see the comment on commit
  23. * 2645af7.
  24. */
  25. exports.items = [{
  26. item: "command",
  27. runAt: "server",
  28. name: "edit",
  29. description: l10n.lookup("editDesc"),
  30. manual: l10n.lookup("editManual2"),
  31. params: [
  32. {
  33. name: "resource",
  34. type: {
  35. name: "resource",
  36. include: "text/css"
  37. },
  38. description: l10n.lookup("editResourceDesc")
  39. },
  40. {
  41. name: "line",
  42. defaultValue: 1,
  43. type: {
  44. name: "number",
  45. min: 1,
  46. step: 10
  47. },
  48. description: l10n.lookup("editLineToJumpToDesc")
  49. }
  50. ],
  51. returnType: "editArgs",
  52. exec: args => {
  53. return { href: args.resource.name, line: args.line };
  54. }
  55. }, {
  56. item: "converter",
  57. from: "editArgs",
  58. to: "dom",
  59. exec: function (args, context) {
  60. let target = context.environment.target;
  61. let toolboxOpened = gDevTools.showToolbox(target, "styleeditor");
  62. return toolboxOpened.then(function (toolbox) {
  63. let styleEditor = toolbox.getCurrentPanel();
  64. styleEditor.selectStyleSheet(args.href, args.line);
  65. return null;
  66. });
  67. }
  68. }];