not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.2 KiB

  1. define(["sugar-web/activity/activity","webL10n","sugar-web/datastore"], function (activity, webL10n, datastore) {
  2. // Manipulate the DOM only when it is ready.
  3. requirejs(['domReady!'], function (doc) {
  4. // Initialize the activity.
  5. activity.setup();
  6. var buttons = ["insertText", "wmd-bold-button-second", "wmd-italic-button-second", "wmd-heading-button", "wmd-hr-button",
  7. "wmd-olist-button", "wmd-ulist-button", "wmd-code-button", "wmd-quote-button", "wmd-link-button",
  8. "wmd-undo-button", "wmd-redo-button", "wmd-showHideEditor-button","wmd-showHidePreview-button"];
  9. inputTextContent = document.getElementById("wmd-input-second");
  10. inputTextContent.value = "#"+l10n_s.get("sample-input");
  11. //to save and resume the contents from datastore.
  12. var datastoreObject = activity.getDatastoreObject();
  13. inputTextContent.onblur = function () {
  14. var jsonData = JSON.stringify((inputTextContent.value).toString());
  15. datastoreObject.setDataAsText(jsonData);
  16. datastoreObject.save(function () {});
  17. };
  18. markdownParsing(); //to load for the first time
  19. datastoreObject.loadAsText(function (error, metadata, data) {
  20. markdowntext = JSON.parse(data);
  21. inputTextContent.value = markdowntext;
  22. markdownParsing(); //to load again when there is a datastore entry
  23. });
  24. for (i = 0; i < buttons.length; i++) {
  25. document.getElementById(buttons[i]).title = l10n_s.get(buttons[i]);
  26. }
  27. var journal = document.getElementById("insertText");
  28. journal.onclick = function () {
  29. activity.showObjectChooser(function (error, result) {
  30. //result1 = result.toString();
  31. var datastoreObject2 = new datastore.DatastoreObject(result);
  32. datastoreObject2.loadAsText(function (error, metadata, data) {
  33. try {
  34. textdata = JSON.parse(data);
  35. } catch (e) {
  36. textdata = data;
  37. }
  38. var inputTextContent = document.getElementById("wmd-input-second");
  39. //inputTextContent.value += textdata;
  40. insertAtCursor(inputTextContent, textdata);
  41. });
  42. });
  43. };
  44. var showHideEditor = document.getElementById("wmd-showHideEditor-button");
  45. var showHidePreview = document.getElementById("wmd-showHidePreview-button");
  46. var panel = document.getElementById("wmd-panel");
  47. var preview = document.getElementById("wmd-preview-second");
  48. var textArea = document.getElementById("wmd-input-second");
  49. function isElementHidden (element) {
  50. return window.getComputedStyle(element, null).getPropertyValue('display') === 'none';
  51. }
  52. showHideEditor.onclick = function(){
  53. if(isElementHidden(preview))
  54. {
  55. preview.style.display = "inline";
  56. preview.style.width = "97%";
  57. panel.style.width = "1%";
  58. }
  59. if(isElementHidden(panel))
  60. {
  61. panel.style.display = "inline";
  62. panel.style.width = "47%";
  63. preview.style.width = "47%";
  64. }
  65. else{
  66. panel.style.display = "none";
  67. panel.style.width = "1%";
  68. preview.style.width = "97%";
  69. }
  70. }
  71. showHidePreview.onclick = function(){
  72. if(isElementHidden(panel))
  73. {
  74. panel.style.display = "inline";
  75. panel.style.width = "97%";
  76. preview.style.width = "1%";
  77. }
  78. if(isElementHidden(preview))
  79. {
  80. preview.style.display = "inline";
  81. preview.style.width = "47%";
  82. panel.style.width = "47%";
  83. }
  84. else{
  85. preview.style.display = "none";
  86. preview.style.width = "1%";
  87. panel.style.width = "94%";
  88. }
  89. }
  90. function insertAtCursor(myField, myValue) {
  91. //IE support
  92. if (document.selection) {
  93. myField.focus();
  94. sel = document.selection.createRange();
  95. sel.text = myValue;
  96. }
  97. //MOZILLA and others
  98. else if (myField.selectionStart || myField.selectionStart == '0') {
  99. var startPos = myField.selectionStart;
  100. var endPos = myField.selectionEnd;
  101. myField.value = myField.value.substring(0, startPos)
  102. + myValue
  103. + myField.value.substring(endPos, myField.value.length);
  104. } else {
  105. myField.value += myValue;
  106. }
  107. //markdownParsing();
  108. }
  109. function markdownParsing() {
  110. var converter2 = new Markdown.Converter();
  111. var help = function () {
  112. alert(l10n_s.get("need-help"));
  113. }
  114. var options = {
  115. helpButton: {
  116. handler: help
  117. },
  118. strings: {
  119. quoteexample: l10n_s.get("put-it-right-here")
  120. }
  121. };
  122. var editor2 = new Markdown.Editor(converter2, "-second", options);
  123. editor2.run();
  124. }
  125. });
  126. });