gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<script lang="ts" setup>
import type { MpDraftApi } from '#/api/mp/draft';
 
import { computed, ref } from 'vue';
 
import { confirm } from '../../../../packages/effects/common-ui/src';
import { IconifyIcon } from '../../../../packages/icons/src';
 
import { Button, Col, Input, Layout, Row, Textarea } from 'ant-design-vue';
 
import { createEmptyNewsItem } from '#/api/mp/draft';
import { Tinymce as RichTextarea } from '#/components/tinymce';
 
import CoverSelect from './cover-select.vue';
 
defineOptions({ name: 'NewsForm' });
 
const props = defineProps<{
  isCreating: boolean;
  modelValue: MpDraftApi.NewsItem[] | null;
}>();
 
const emit = defineEmits<{
  (e: 'update:modelValue', v: MpDraftApi.NewsItem[]): void;
}>();
 
const newsList = computed<MpDraftApi.NewsItem[]>({
  get() {
    return props.modelValue === null
      ? [createEmptyNewsItem()]
      : props.modelValue;
  },
  set(val) {
    emit('update:modelValue', val);
  },
});
 
const activeNewsIndex = ref(0);
const activeNewsItem = computed(() => {
  const item = newsList.value[activeNewsIndex.value];
  if (!item) {
    return createEmptyNewsItem();
  }
  return item;
});
 
/** 将图文向下移动 */
function moveDownNews(index: number) {
  const current = newsList.value[index];
  const next = newsList.value[index + 1];
  if (current && next) {
    newsList.value[index] = next;
    newsList.value[index + 1] = current;
    activeNewsIndex.value = index + 1;
  }
}
 
/** 将图文向上移动 */
function moveUpNews(index: number) {
  const current = newsList.value[index];
  const prev = newsList.value[index - 1];
  if (current && prev) {
    newsList.value[index] = prev;
    newsList.value[index - 1] = current;
    activeNewsIndex.value = index - 1;
  }
}
 
/** 删除指定 index 的图文 */
async function removeNews(index: number) {
  await confirm('确定删除该图文吗?');
  newsList.value.splice(index, 1);
  if (activeNewsIndex.value === index) {
    activeNewsIndex.value = 0;
  }
}
 
/** 添加一个图文 */
function plusNews() {
  newsList.value.push(createEmptyNewsItem());
  activeNewsIndex.value = newsList.value.length - 1;
}
</script>
 
<template>
  <Layout>
    <Layout.Sider width="40%" theme="light">
      <div class="mx-auto mb-[10px] w-[60%] border border-[#eaeaea] p-[10px]">
        <div v-for="(news, index) in newsList" :key="index">
          <div
            class="group relative mx-auto mb-[10px] w-full cursor-pointer border-[2px] bg-white"
            v-if="index === 0"
            :class="
              activeNewsIndex === index
                ? 'border-green-500'
                : 'border-transparent'
            "
            @click="activeNewsIndex = index"
          >
            <div class="relative w-full bg-[#acadae]">
              <img
                class="max-h-[200px] min-h-[100px] w-full object-cover"
                :src="news.thumbUrl"
              />
              <div
                class="absolute bottom-0 left-0 mb-[5px] ml-[5px] inline-block h-[25px] w-[100%] overflow-hidden text-ellipsis whitespace-nowrap p-[1%] text-[18px] text-white"
              >
                {{ news.title }}
              </div>
            </div>
            <div
              class="absolute bottom-0 right-[-45px] top-0 flex flex-col justify-center gap-[10px] py-[5px] text-center"
              v-if="newsList.length > 1"
            >
              <Button
                type="default"
                shape="circle"
                size="small"
                @click="() => moveDownNews(index)"
              >
                <IconifyIcon icon="lucide:arrow-down" />
              </Button>
              <Button
                v-if="isCreating"
                type="primary"
                danger
                shape="circle"
                size="small"
                @click="() => removeNews(index)"
              >
                <IconifyIcon icon="lucide:trash-2" />
              </Button>
            </div>
          </div>
          <div
            class="group relative mx-auto mb-[10px] cursor-pointer border-[2px] bg-white"
            v-if="index > 0"
            :class="
              activeNewsIndex === index
                ? 'border-green-500'
                : 'border-transparent'
            "
            @click="activeNewsIndex = index"
          >
            <div class="relative flex items-center justify-between">
              <div
                class="mb-[5px] ml-[5px] h-[25px] flex-1 overflow-hidden text-ellipsis whitespace-nowrap p-[1%] text-[16px]"
              >
                {{ news.title }}
              </div>
              <img
                class="block h-[90px] w-[90px] object-cover"
                :src="news.thumbUrl"
              />
            </div>
            <div
              class="absolute bottom-0 right-[-45px] top-0 flex flex-col justify-center gap-[10px] py-[5px] text-center"
            >
              <Button
                v-if="newsList.length > index + 1"
                shape="circle"
                type="default"
                size="small"
                @click="() => moveDownNews(index)"
              >
                <IconifyIcon icon="lucide:arrow-down" />
              </Button>
              <Button
                v-if="index > 0"
                type="default"
                shape="circle"
                size="small"
                @click="() => moveUpNews(index)"
              >
                <IconifyIcon icon="lucide:arrow-up" />
              </Button>
              <Button
                v-if="isCreating"
                type="primary"
                danger
                size="small"
                shape="circle"
                @click="() => removeNews(index)"
              >
                <IconifyIcon icon="lucide:trash-2" />
              </Button>
            </div>
          </div>
        </div>
        <Row
          justify="center"
          class="mt-[5px] border-t border-[#eaeaea] pt-[5px] text-center"
        >
          <Button
            type="primary"
            shape="circle"
            @click="plusNews"
            v-if="newsList.length < 8 && isCreating"
          >
            <IconifyIcon icon="lucide:plus" />
          </Button>
        </Row>
      </div>
    </Layout.Sider>
    <Layout.Content class="bg-white">
      <div v-if="newsList.length > 0 && activeNewsItem">
        <!-- 标题、作者、原文地址 -->
        <Row :gutter="20">
          <Col :span="24">
            <Input
              v-model:value="activeNewsItem.title"
              placeholder="请输入标题(必填)"
            />
          </Col>
          <Col :span="24" class="mt-[5px]">
            <Input
              v-model:value="activeNewsItem.author"
              placeholder="请输入作者"
            />
          </Col>
          <Col :span="24" class="mt-[5px]">
            <Input
              v-model:value="activeNewsItem.contentSourceUrl"
              placeholder="请输入原文地址"
            />
          </Col>
        </Row>
        <!-- 封面和摘要 -->
        <Row :gutter="20">
          <Col :span="12">
            <CoverSelect
              v-model="activeNewsItem"
              :is-first="activeNewsIndex === 0"
            />
          </Col>
          <Col :span="12">
            <p>摘要:</p>
            <Textarea
              :rows="8"
              v-model:value="activeNewsItem.digest"
              placeholder="请输入摘要"
              class="inline-block w-full align-top"
              :maxlength="120"
              :show-count="true"
            />
          </Col>
        </Row>
        <!--富文本编辑器组件-->
        <Row>
          <Col :span="24">
            <RichTextarea v-model="activeNewsItem.content" />
          </Col>
        </Row>
      </div>
    </Layout.Content>
  </Layout>
</template>
 
<style lang="scss" scoped>
:deep(.ant-row) {
  margin-bottom: 20px;
}
 
:deep(.ant-row:last-child) {
  margin-bottom: 0;
}
</style>