customEditor.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>Editor</title>
  7. </head>
  8. <body>
  9. <h1>Custom Editor Instance</h1>
  10. <p>This is an example of creating a custom editor. You can edit the text below and if you hit save, it will replace the source file.</p>
  11. <textarea id="source" rows="10" cols="80"></textarea>
  12. <script>
  13. var code;
  14. //get a reference to the element
  15. var txt = document.getElementById("source");
  16. txt.addEventListener("input", (event) => {
  17. atomicQueryPromise("editorChange");
  18. });
  19. /**
  20. * Promise version of atomic query
  21. * @param {string} message the query to use to pass to atomicQuery. If there is no payload, this will be passed directly, otherwise it will be passed in a data object
  22. * @param {any} payload optional data to send
  23. * @return {Promise}
  24. */
  25. function atomicQueryPromise(message) {
  26. return new Promise(function(resolve, reject) {
  27. var queryMessage = message;
  28. // if message is coming in as an object then let's stringify it
  29. if (typeof (message) != "string") {
  30. queryMessage = JSON.stringify(message);
  31. }
  32. window.atomicQuery({
  33. request: queryMessage,
  34. persistent: false,
  35. onSuccess: resolve,
  36. onFailure: (error_code, error_message) => reject({ error_code: error_code, error_message: error_message })
  37. });
  38. });
  39. }
  40. /**
  41. * Queries the host for a particular resource and returns it in a promise
  42. * @param {string} codeUrl
  43. * @return {Promise}
  44. */
  45. function getResource(codeUrl) {
  46. return new Promise(function(resolve, reject) {
  47. var xmlHttp = new XMLHttpRequest();
  48. xmlHttp.onreadystatechange = () => {
  49. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  50. resolve(xmlHttp.responseText);
  51. }
  52. };
  53. xmlHttp.open("GET", codeUrl, true); // true for asynchronous
  54. xmlHttp.send(null);
  55. });
  56. }
  57. // Functions exposed to the host editor. These
  58. // are hooked in here so that they are available immediately from the host
  59. // and when called will bring in the interop as a promise and call it once
  60. // it has been loaded
  61. function HOST_loadCode(url) {
  62. getResource(url).then((code)=>{
  63. txt.value = code;
  64. });
  65. }
  66. function HOST_saveCode() {
  67. atomicQueryPromise({
  68. message: "editorSaveCode",
  69. payload: txt.value
  70. });
  71. }
  72. function HOST_resourceRenamed(path, newPath) {
  73. alert("Resource Renamed: " + path + "->" + newPath);
  74. }
  75. function HOST_resourceDeleted(path) {
  76. alert("Resource Deleted: " + path);
  77. }
  78. function HOST_loadPreferences(prefUrl) {
  79. alert("Prefs Changed: " + prefUrl);
  80. }
  81. </script>
  82. </body>
  83. </html>