graceEditor.nvue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="grace-editor">
  3. <textarea class="grace-editor-title" v-model="article.title" placeholder="# 请输入标题" auto-height />
  4. <!-- 内容区域 -->
  5. <view v-for="(item, index) in article.contents" :key="index" class="grace-editor-items">
  6. <!-- 普通文本 -->
  7. <textarea class="grace-editor-txt" maxlength="-1" :data-index="index" v-if="item.type == 'txt'"
  8. @input="graceEditorInput" :value="item.content" placeholder="# 请在此处输入文本内容" auto-height />
  9. <!-- 居中标题 -->
  10. <input type="text" class="grace-editor-center" maxlength="-1" :data-index="index"
  11. @input="graceEditorInput" :value="item.content" placeholder="# 输入居中题目" v-else-if="item.type == 'center'" />
  12. <!-- 图片 -->
  13. <view class="grace-editor-img-wrap" v-else-if="item.type == 'img'">
  14. <image :src="item.content" class="grace-editor-img" :data-index="index" mode="widthFix" @tap="deleteItem"></image>
  15. <view class="grace-editor-img-error" v-if="item.error">
  16. <view class="grace-flex-center">
  17. <text class="grace-editor-img-error-text grace-red" style="font-size:32rpx;">图片上传失败!</text>
  18. </view>
  19. <view class="grace-flex-center grace-margin-top">
  20. <text class="grace-editor-img-error-text grace-green">点击发布按钮重新上传</text>
  21. <text class="grace-editor-img-error-text">或</text>
  22. <text class="grace-editor-img-error-text grace-red" :data-index="index" @tap.stop="deleteItem">删除图片</text>
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 引用 -->
  27. <textarea class="grace-editor-quote" maxlength="-1" :data-index="index" v-else-if="item.type == 'quote'"
  28. @input="graceEditorInput" :value="item.content" placeholder="# 请在此处输入引用内容" auto-height />
  29. <!-- 加粗 -->
  30. <textarea class="grace-editor-strong" :data-index="index" maxlength="-1" v-else-if="item.type == 'strong'"
  31. @input="graceEditorInput" :value="item.content" placeholder="# 请在此处输入加粗内容" auto-height />
  32. <!-- 链接 -->
  33. <input type="text" class="grace-editor-link" :data-index="index" v-else-if="item.type == 'link'"
  34. @input="graceEditorInput" :value="item.content" placeholder="# 输入连接地址" />
  35. <!-- 分割 -->
  36. <text class="grace-editor-spline" :data-index="index" @tap="deleteItem" v-else-if="item.type == 'spline'">● ● ●</text>
  37. </view>
  38. <!-- 选项类型选择 -->
  39. <view class="grace-space-between grace-editor-menus" :style="{paddingBottom:ipxHeight}">
  40. <text class="grace-editor-icons grace-icons" data-type="txt" @tap="graceEditorAddItem">&#xe9e4;</text>
  41. <text class="grace-editor-icons grace-icons" data-type="center" @tap="graceEditorAddItem">&#xe621;</text>
  42. <text class="grace-editor-icons grace-icons" data-type="img" @tap="graceEditorAddItem">&#xe63d;</text>
  43. <text class="grace-editor-icons grace-icons" data-type="quote" @tap="graceEditorAddItem">&#xe620;</text>
  44. <text class="grace-editor-icons grace-icons" data-type="strong" @tap="graceEditorAddItem">&#xe640;</text>
  45. <text class="grace-editor-icons grace-icons" data-type="link" @tap="graceEditorAddItem">&#xe61e;</text>
  46. <text class="grace-editor-icons grace-icons" data-type="spline" @tap="graceEditorAddItem">&#xe61b;</text>
  47. </view>
  48. <!-- 底部占位 -->
  49. <view style="height:200rpx; width:100%"></view>
  50. </view>
  51. </template>
  52. <script>
  53. export default{
  54. data() {
  55. return {
  56. article : {title:'', contents:[]},
  57. ipxHeight : 0
  58. }
  59. },
  60. methods:{
  61. graceEditorAddItem : function(e){
  62. var type = e.currentTarget.dataset.type;
  63. if(type == 'img'){
  64. uni.chooseImage({
  65. success:(e)=>{
  66. var imgs = e.tempFilePaths;
  67. for(let i = 0; i < imgs.length; i++){
  68. this.article.contents.push({type:type,content:imgs[i]});
  69. }
  70. this.returnArt();
  71. }
  72. });
  73. }else{
  74. this.article.contents.push({type:type,content:''});
  75. this.returnArt();
  76. }
  77. },
  78. graceEditorInput : function(e){
  79. var index = e.currentTarget.dataset.index;
  80. var val = e.detail.value;
  81. if(val == ''){
  82. this.article.contents.splice(index, 1);
  83. }else{
  84. this.article.contents[index].content = val;
  85. }
  86. this.returnArt();
  87. },
  88. deleteItem : function(e){
  89. var index = e.currentTarget.dataset.index;
  90. uni.showModal({
  91. title:"提示",
  92. content:"确定要删除项目吗?",
  93. success:(e)=>{
  94. if(e.confirm){this.article.contents.splice(index, 1); this.returnArt();}
  95. }
  96. })
  97. },
  98. returnArt : function(){
  99. this.$emit('change', this.article);
  100. },
  101. setError : function(index){
  102. this.article.contents[index].error = true;
  103. this.article.contents.splice(index, 1, this.article.contents[index]);
  104. },
  105. setDefault : function (article) {
  106. this.article = article;
  107. this.returnArt();
  108. }
  109. }
  110. }
  111. </script>
  112. <style>
  113. /* 编辑器 */
  114. .grace-editor{padding:10rpx 25rpx; width:750rpx;}
  115. .grace-editor-title{padding:25rpx 0; border-bottom-width:1px; border-bottom-style:solid; border-color:#F1F2F3; width:700rpx; font-size:36rpx; font-weight:bold; line-height:50rpx; color:#555555;}
  116. .grace-editor-menus{width:750rpx; position:fixed; left:0; background-color:#F8F8F8; bottom:0; border-top-width:1px; border-top-style:solid; border-color:#F1F2F3; padding:0 25rpx;}
  117. .grace-editor-icons{width:700rpx; flex:1; height:88rpx; color:#666666; line-height:88rpx; text-align:center; font-size:38rpx !important;}
  118. .grace-editor-items{margin-top:10rpx; flex-direction:row; justify-content:center;}
  119. .grace-editor-txt{width:700rpx; font-size:26rpx; margin-top:15rpx; margin-bottom:15rpx;}
  120. .grace-editor-center{width:300rpx; text-align:center; font-size:28rpx; color:#333333; line-height:60rpx; padding:10rpx; border-bottom-width:1px; border-bottom-style:solid; border-color:#F1F2F3; font-weight:bold;}
  121. .grace-editor-img-wrap{width:700rpx; height:300rpx; overflow:hidden; margin:10rpx 0; position:relative; font-size:0;}
  122. .grace-editor-img{width:700rpx; height:300rpx;}
  123. .grace-editor-img-error{position:absolute; width:700rpx; height:300rpx; left:0; top:0; background-color:rgba(0,0,0,0.5); flex-direction:column; align-items:center; justify-content:center;}
  124. .grace-editor-img-error-text{font-size:28rpx; margin:0 15rpx; color:#FFFFFF;}
  125. .grace-editor-quote{border-radius:8rpx; width:700rpx; color:#333333; background-color:#F8F8F8; margin-top:15rpx; margin-bottom:15rpx; font-size:26rpx;}
  126. .grace-editor-strong{width:700rpx; font-weight:bold; font-size:26rpx; margin-top:15rpx; margin-bottom:15rpx;}
  127. .grace-editor-link{width:700rpx; font-size:26rpx; margin-top:15rpx; margin-bottom:15rpx; color:#007AFF;}
  128. .grace-editor-spline{width:700rpx; line-height:60rpx; text-align:center; color:#8788A3; font-size:28rpx; opacity:0.6;}
  129. </style>