mdn.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "use strict";
  5. const l10n = require("gcli/l10n");
  6. var MdnDocsWidget;
  7. try {
  8. MdnDocsWidget = require("devtools/client/shared/widgets/MdnDocsWidget");
  9. } catch (e) {
  10. // DevTools MdnDocsWidget only available in Firefox Desktop
  11. }
  12. exports.items = [{
  13. name: "mdn",
  14. description: l10n.lookup("mdnDesc")
  15. }, {
  16. item: "command",
  17. runAt: "client",
  18. name: "mdn css",
  19. description: l10n.lookup("mdnCssDesc"),
  20. returnType: "cssPropertyOutput",
  21. params: [{
  22. name: "property",
  23. type: { name: "string" },
  24. defaultValue: null,
  25. description: l10n.lookup("mdnCssProp")
  26. }],
  27. exec: function(args) {
  28. if (!MdnDocsWidget) {
  29. return null;
  30. }
  31. return MdnDocsWidget.getCssDocs(args.property).then(result => {
  32. return {
  33. data: result,
  34. url: MdnDocsWidget.PAGE_LINK_URL + args.property,
  35. property: args.property
  36. };
  37. }, error => {
  38. return { error, property: args.property };
  39. });
  40. }
  41. }, {
  42. item: "converter",
  43. from: "cssPropertyOutput",
  44. to: "dom",
  45. exec: function(result, context) {
  46. let propertyName = result.property;
  47. let document = context.document;
  48. let root = document.createElement("div");
  49. if (result.error) {
  50. // The css property specified doesn't exist.
  51. root.appendChild(document.createTextNode(
  52. l10n.lookupFormat("mdnCssPropertyNotFound", [ propertyName ]) +
  53. " (" + result.error + ")"));
  54. } else {
  55. let title = document.createElement("h2");
  56. title.textContent = propertyName;
  57. root.appendChild(title);
  58. let link = document.createElement("p");
  59. link.classList.add("gcli-mdn-url");
  60. link.textContent = l10n.lookup("mdnCssVisitPage");
  61. root.appendChild(link);
  62. link.addEventListener("click", () => {
  63. let mainWindow = context.environment.chromeWindow;
  64. mainWindow.openUILinkIn(result.url, "tab");
  65. });
  66. let summary = document.createElement("p");
  67. summary.textContent = result.data.summary;
  68. root.appendChild(summary);
  69. }
  70. return root;
  71. }
  72. }];