sunEdt.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div>
  3. <div id="editor"></div>
  4. </div>
  5. </template>
  6. <script setup>
  7. import SunEditor from "suneditor";
  8. import "suneditor/dist/css/suneditor.min.css";
  9. import plugins from "suneditor/src/plugins";
  10. // Props로 초기 데이터를 받음
  11. const props = defineProps({
  12. initialContent: {
  13. type: String,
  14. default: "", // 초기값 설정
  15. },
  16. });
  17. const editorInstance = ref(null);
  18. // SunEditor 초기화
  19. onMounted(() => {
  20. const editorElement = document.getElementById("editor");
  21. if (editorElement) {
  22. editorInstance.value = SunEditor.create(editorElement, {
  23. plugins: plugins,
  24. buttonList: [
  25. [
  26. //"undo",
  27. //"redo",
  28. //"font",
  29. "fontSize",
  30. //"formatBlock",
  31. "paragraphStyle",
  32. "blockquote",
  33. "bold",
  34. "underline",
  35. "italic",
  36. "strike",
  37. //"subscript",
  38. //"superscript",
  39. "fontColor",
  40. //"hiliteColor",
  41. //"textStyle",
  42. //"removeFormat",
  43. "outdent",
  44. "indent",
  45. "align",
  46. "horizontalRule",
  47. "list",
  48. "lineHeight",
  49. //"table",
  50. "link",
  51. "image",
  52. //"video",
  53. //"audio",
  54. "fullScreen",
  55. //"showBlocks",
  56. //"codeView",
  57. //"preview",
  58. //"print",
  59. //"save",
  60. //"template",
  61. ],
  62. ],
  63. height: "50vh",
  64. });
  65. // 초기 content 설정
  66. if (props.initialContent) {
  67. editorInstance.value.setContents(props.initialContent);
  68. }
  69. } else {
  70. console.error("Editor element not found");
  71. }
  72. });
  73. const getEditorContent = () => {
  74. if (editorInstance.value) {
  75. return editorInstance.value.getContents(); // SunEditor에서 편집된 내용 가져오기
  76. }
  77. return "";
  78. };
  79. defineExpose({
  80. getEditorContent,
  81. });
  82. // 초기 content 변경 시 SunEditor 업데이트
  83. watch(
  84. () => props.initialContent,
  85. (newContent) => {
  86. if (editorInstance.value) {
  87. editorInstance.value.setContents(newContent || "");
  88. }
  89. }
  90. );
  91. </script>
  92. <style scoped>
  93. /* 스타일이 필요하면 여기에 추가 */
  94. </style>