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.

279 lines
9.6 KiB

  1. {% comment %}
  2. Makes an embedded editor example with tabs, including an "Edit on CodePen" tab.
  3. params:
  4. id (required). Picks folder for files, and used in css classes.
  5. type (optional) - Specifies if the example is `tinymce` or `tinydrive` specific default is `tinymce`
  6. tab (optional - default to "run"), "run", "html", "css" or "js". The first tab to open.
  7. height (optional - no default). min-height in pixels.
  8. script_url_override (optional - no default).
  9. - Override the full tinymce.min.js URL, including api key.
  10. - Useful for testing things that aren't in the main channel, yet.
  11. - Remove this setting once the feature is in the main channel.
  12. Files required under _includes/live-demos/id/
  13. index.html
  14. index.js
  15. index.css optional file
  16. example.js optional file
  17. if index.css is omitted, the css tab will not display
  18. example.js purpose:
  19. when the file is present the live-demo js will display the contents of example.js, while the real demo code executes with index.js
  20. Useful for when we want to hide actual api-keys or tokens: 'example-token'.
  21. When the example.js file is present, the link to the external codepen site is disabled
  22. {% endcomment %}
  23. {% comment %} Add cloud script(s) {% endcomment %}
  24. {% if include.script_url_override %}
  25. <script src='{{ include.script_url_override }}'></script>
  26. {% else %}
  27. {% if include.type == 'tinydrive' %}
  28. {% include tinydrive-script-tag-once.html %}
  29. {% else %}
  30. {% include tinymce-script-tag-once.html %}
  31. {% endif %}
  32. {% endif %}
  33. {% comment %} Check if a example.js exists {% endcomment %}
  34. {% capture hasExampleJS %}
  35. {% file_exists _includes/live-demos/{{ include.id }}/example.js %}
  36. {% endcapture %}
  37. {% comment %} Check if a example.html exists {% endcomment %}
  38. {% capture hasExampleHTML %}
  39. {% file_exists _includes/live-demos/{{ include.id }}/example.html %}
  40. {% endcapture %}
  41. {% comment %} Assign html to live_demo_html {% endcomment %}
  42. {% capture live_demo_html %}
  43. {% include live-demos/{{ include.id }}/index.html %}
  44. {% endcapture %}
  45. {% comment %} Assign js to live_demo_js {% endcomment %}
  46. {% capture live_demo_js %}
  47. {% include live-demos/{{ include.id }}/index.js %}
  48. {% endcapture %}
  49. {% comment %} If example.js exists, Assign to example_js else use index.js {% endcomment %}
  50. {% if hasExampleJS contains "true" %}
  51. {% capture example_js %}
  52. {% include live-demos/{{ include.id }}/example.js %}
  53. {% endcapture %}
  54. {% else %}
  55. {% capture example_js %}
  56. {% include live-demos/{{ include.id }}/index.js %}
  57. {% endcapture %}
  58. {% endif %}
  59. {% comment %} If example.html exists, Assign to example_html else use index.html {% endcomment %}
  60. {% if hasExampleHTML contains "true" %}
  61. {% capture example_html %}
  62. {% include live-demos/{{ include.id }}/example.html %}
  63. {% endcapture %}
  64. {% else %}
  65. {% capture example_html %}
  66. {% include live-demos/{{ include.id }}/index.html %}
  67. {% endcapture %}
  68. {% endif %}
  69. {% comment %} Check if a style.css exists {% endcomment %}
  70. {% capture hasCss %}
  71. {% file_exists _includes/live-demos/{{ include.id }}/style.css %}
  72. {% endcapture %}
  73. {% comment %} If style.css has been provided... {% endcomment %}
  74. {% if hasCss contains "true" %}
  75. {% comment %} Assign CSS to live_demo_css {% endcomment %}
  76. {% capture live_demo_css %}
  77. {% include live-demos/{{ include.id }}/style.css %}
  78. {% endcapture %}
  79. {% comment %} Create and assign a CSS codeblock to live_demo_css_preview {% endcomment %}
  80. {% capture live_demo_css_preview %}
  81. ```css
  82. {{ live_demo_css }}
  83. ```
  84. {% endcapture %}
  85. {% endif %}
  86. {% comment %} Create and assign a HTML codeblock to live_demo_html_preview {% endcomment %}
  87. {% capture live_demo_html_preview %}
  88. ```html
  89. {{ example_html }}
  90. ```
  91. {% endcapture %}
  92. {% comment %} Create and assign a JS codeblock to live_demo_js_preview {% endcomment %}
  93. {% capture live_demo_js_preview %}
  94. ```js
  95. {{ example_js }}
  96. ```
  97. {% endcapture %}
  98. {% comment %} Changes primary tab name where type is provided {% endcomment %}
  99. {% if include.type == 'tinydrive' %}
  100. {% assign runText = "Tiny Drive" %}
  101. {% else %}
  102. {% assign runText = "TinyMCE" %}
  103. {% endif %}
  104. {% comment %} Changes initial tab where tab is specified {% endcomment %}
  105. {% if include.tab %}
  106. {% assign initialTab = include.tab %}
  107. {% else %}
  108. {% assign initialTab = "run" %}
  109. {% endif %}
  110. {% comment %} Creates the live-demo tabs {% endcomment %}
  111. <div id="live_demo_{{ include.id }}" class="live_demo" {% if include.height %}style="min-height:{{ include.height }}px;"{% endif %}>
  112. <div class="live_demo_tabs">
  113. {% for tab in site.data.live-demo-tabs %}
  114. {% if tab.name == initialTab %}
  115. {% assign class="live_demo_tab_selected" %}
  116. {% else %}
  117. {% assign class="live_demo_tab_deselected" %}
  118. {% endif %}
  119. {% if tab.name != "css" or hasCss contains "true" %}
  120. {% if tab.name == "run" %}
  121. <a href="#" id="live_demo_tab_{{ tab.name }}_{{ include.id }}" class="{{ class }}">{{ runText }}</a>
  122. {% else %}
  123. <a href="#" id="live_demo_tab_{{ tab.name }}_{{ include.id }}" class="{{ class }}">{{ tab.text }}</a>
  124. {% endif %}
  125. {% endif %}
  126. {% endfor %}
  127. {% if hasExampleJS contains 'false' %}
  128. <a href="#" id="live_demo_tab_codepen_{{ include.id }}" class="live_demo_tab_deselected ie11_optional">Edit on CodePen.io</a>
  129. {% endif %}
  130. </div>
  131. {% comment %} Adds style.css to page if provided {% endcomment %}
  132. <div id="live_demo_pane_run_{{ include.id }}" {% if "run" != initialTab %}style="display:none;"{% endif %}>
  133. {% if hasCss contains "true" %}
  134. <style type="text/css">
  135. {{ live_demo_css }}
  136. </style>
  137. {% endif %}
  138. {{ live_demo_html }}
  139. </div>
  140. {% comment %} Adds HTML tab content {% endcomment %}
  141. <div id="live_demo_pane_html_{{ include.id }}" {% if "html" != initialTab %}style="display:none;"{% endif %}>
  142. {{ live_demo_html_preview | markdownify }}
  143. </div>
  144. {% comment %} If style.css exists, add CSS tab content {% endcomment %}
  145. {% if hasCss contains "true" %}
  146. <div id="live_demo_pane_css_{{ include.id }}" {% if "css" != initialTab %}style="display:none;"{% endif %}>
  147. {{ live_demo_css_preview | markdownify }}
  148. </div>
  149. {% endif %}
  150. {% comment %} Add content to JS tab from index.js or example.js {% endcomment %}
  151. <div id="live_demo_pane_js_{{ include.id }}" {% if "js" != initialTab %}style="display:none;"{% endif %}>
  152. {{ live_demo_js_preview | markdownify }}
  153. </div>
  154. </div>
  155. {% comment %} Changes initial tab where tab is specified {% endcomment %}
  156. <form action="https://codepen.io/pen/define" method="POST" target="_blank" id="codepen_form_{{ include.id }}">
  157. <input type="hidden" name="data" id="codepen_data_{{ include.id }}" />
  158. </form>
  159. <script>
  160. (function() {
  161. var isIE = !!window.MSInputMethodContext && !!document.documentMode;
  162. if (isIE && document.getElementsByClassName("ie11_optional")[0] !== undefined) {
  163. document.getElementsByClassName("ie11_optional")[0].style.display = 'none';
  164. }
  165. })();
  166. {% comment %} Add index.js to the page (primary tab content) {% endcomment %}
  167. (function() {
  168. {{ live_demo_js }}
  169. })();
  170. (function() {
  171. /* TODO: more js, less jekyll */
  172. var id = "{{ include.id }}";
  173. {% comment %} Add index.js and HTML to variables {% endcomment %}
  174. var html = decodeURIComponent("{{ live_demo_html | uri_escape }}");
  175. var js = decodeURIComponent("{{ live_demo_js | uri_escape }}");
  176. {% comment %} If style.css provided, add to variable {% endcomment %}
  177. {% comment %} set which tabz are shown in on codepen site {% endcomment %}
  178. {% if hasCss contains "true" %}
  179. var css = decodeURIComponent("{{ live_demo_css | uri_escape }}");
  180. var tabNames = ["run","html","css","js"];
  181. {% else %}
  182. var css = "";
  183. var tabNames = ["run","html","js"];
  184. {% endif %}
  185. {% comment %} Data to send to codepen dot io via form input {% endcomment %}
  186. /* Note: there are some other fields we could populate here to polish this. */
  187. /* See: https://blog.codepen.io/documentation/api/prefill/ */
  188. var data = {
  189. title: "{{ runText }} Example",
  190. description: '',
  191. html: html,
  192. css: css,
  193. js: js,
  194. js_external: '{{ site.tinymce_live_demo_url }}'
  195. };
  196. document.getElementById("codepen_data_{{ include.id }}").value = JSON.stringify(data);
  197. {% comment %} Below is just tab selection/change logic {% endcomment %}
  198. /* TODO: */
  199. var tabs = tabNames.map(function(t) {
  200. return {
  201. name: t,
  202. tab: document.getElementById("live_demo_tab_" + t + "_" + id),
  203. pane: document.getElementById("live_demo_pane_" + t + "_" + id)
  204. };
  205. });
  206. var lastTabName = '{{ initialTab }}';
  207. tabs.forEach(function(t) {
  208. t.tab.onclick = function(e) {
  209. if (lastTabName !== t.name) {
  210. tabs.forEach(function(tt) {
  211. tt.pane.style.display = t === tt ? 'block' : 'none';
  212. tt.tab.className = t === tt ? 'live_demo_tab_selected' : 'live_demo_tab_deselected';
  213. });
  214. {% if include.type != 'tinydrive' %}
  215. var editor = tinymce.get('{{ include.id }}');
  216. if (editor) {
  217. if (t.name === 'run') {
  218. editor.show();
  219. } else if (lastTabName === 'run') {
  220. editor.hide();
  221. }
  222. }
  223. {% endif %}
  224. lastTabName = t.name;
  225. }
  226. e.preventDefault();
  227. };
  228. });
  229. if (document.getElementById("live_demo_tab_codepen_" + id) !== null) {
  230. document.getElementById("live_demo_tab_codepen_" + id).onclick = function() {
  231. document.getElementById("codepen_form_" + id).submit();
  232. };
  233. }
  234. })();
  235. </script>