strapi-plugin-copy-any-component 1.0.4 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +5 -4
  2. package/strapi-server.js +94 -49
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-plugin-copy-any-component",
3
- "version": "1.0.4",
3
+ "version": "1.0.7",
4
4
  "description": "A powerful Strapi plugin that allows you to copy and reorder components (sections) between pages using an intuitive drag-and-drop interface. Works with any content type and dynamic zone - no code required!",
5
5
  "keywords": [
6
6
  "strapi",
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "main": "./strapi-server.js",
23
23
  "exports": {
24
+ "./package.json": "./package.json",
24
25
  "./strapi-admin": "./strapi-admin.js",
25
26
  "./strapi-server": "./strapi-server.js"
26
27
  },
@@ -44,9 +45,9 @@
44
45
  },
45
46
  "dependencies": {},
46
47
  "peerDependencies": {
47
- "@strapi/strapi": ">=5.0.0",
48
- "@strapi/design-system": ">=2.0.0",
49
- "@strapi/icons": ">=2.0.0",
48
+ "@strapi/strapi": "^5.0.0",
49
+ "@strapi/design-system": "^2.0.0-rc.1 || ^2.0.0",
50
+ "@strapi/icons": "^2.0.0-rc.1 || ^2.0.0",
50
51
  "react": "^18.0.0",
51
52
  "react-dom": "^18.0.0"
52
53
  },
package/strapi-server.js CHANGED
@@ -207,17 +207,38 @@ const controller = ({ strapi }) => ({
207
207
  const contentType = pluginConfig.contentType || 'api::page.page';
208
208
  const dynamicZoneField = pluginConfig.dynamicZoneField || 'sections';
209
209
 
210
- const pages = await strapi.entityService.findMany(contentType, {
211
- populate: [dynamicZoneField],
212
- });
210
+ // Content type'ın kind'ını kontrol et
211
+ const contentTypeModel = strapi.contentTypes[contentType];
212
+ const isSingleType = contentTypeModel?.kind === 'singleType';
213
+
214
+ let pages;
215
+ if (isSingleType) {
216
+ // SingleType için findOne kullan
217
+ try {
218
+ const page = await strapi.entityService.findOne(contentType, {
219
+ populate: [dynamicZoneField],
220
+ });
221
+ pages = page ? [page] : [];
222
+ } catch (error) {
223
+ // Eğer singleType henüz oluşturulmamışsa boş array döndür
224
+ pages = [];
225
+ }
226
+ } else {
227
+ // CollectionType için findMany kullan
228
+ pages = await strapi.entityService.findMany(contentType, {
229
+ populate: [dynamicZoneField],
230
+ });
231
+ }
232
+
213
233
  const formattedPages = pages.map((page) => ({
214
234
  ...page,
215
235
  documentId: page.documentId || page.id,
216
236
  }));
217
237
  ctx.body = { data: formattedPages };
218
238
  } catch (error) {
239
+ strapi.log.error("Error in getPages:", error);
219
240
  ctx.status = 500;
220
- ctx.body = { error: error.message };
241
+ ctx.body = { error: error.message || "Unknown error" };
221
242
  }
222
243
  },
223
244
 
@@ -229,30 +250,49 @@ const controller = ({ strapi }) => ({
229
250
  const contentType = pluginConfig.contentType || 'api::page.page';
230
251
  const dynamicZoneField = pluginConfig.dynamicZoneField || 'sections';
231
252
 
253
+ // Content type'ın kind'ını kontrol et
254
+ const contentTypeModel = strapi.contentTypes[contentType];
255
+ const isSingleType = contentTypeModel?.kind === 'singleType';
256
+
232
257
  let page;
233
- const numericId = parseInt(pageId);
234
- if (!isNaN(numericId)) {
235
- try {
236
- page = await strapi.entityService.findOne(contentType, numericId, {
237
- populate: [dynamicZoneField],
238
- });
239
- } catch (error) {
240
- // Try with documentId
241
- }
242
- }
243
258
 
244
- if (!page) {
259
+ if (isSingleType) {
260
+ // SingleType için direkt findOne kullan (ID gerekmez)
245
261
  try {
246
- const pages = await strapi.entityService.findMany(contentType, {
247
- filters: { documentId: pageId },
262
+ page = await strapi.entityService.findOne(contentType, {
248
263
  populate: [dynamicZoneField],
249
264
  });
250
- page = pages[0];
251
- } catch (err) {
265
+ } catch (error) {
252
266
  ctx.status = 404;
253
267
  ctx.body = { error: "Page not found: " + pageId, data: null };
254
268
  return;
255
269
  }
270
+ } else {
271
+ // CollectionType için documentId ile bul
272
+ const numericId = parseInt(pageId);
273
+ if (!isNaN(numericId)) {
274
+ try {
275
+ page = await strapi.entityService.findOne(contentType, numericId, {
276
+ populate: [dynamicZoneField],
277
+ });
278
+ } catch (error) {
279
+ // Try with documentId
280
+ }
281
+ }
282
+
283
+ if (!page) {
284
+ try {
285
+ const pages = await strapi.entityService.findMany(contentType, {
286
+ filters: { documentId: pageId },
287
+ populate: [dynamicZoneField],
288
+ });
289
+ page = pages[0];
290
+ } catch (err) {
291
+ ctx.status = 404;
292
+ ctx.body = { error: "Page not found: " + pageId, data: null };
293
+ return;
294
+ }
295
+ }
256
296
  }
257
297
 
258
298
  if (!page) {
@@ -307,7 +347,23 @@ const controller = ({ strapi }) => ({
307
347
  const contentType = pluginConfig.contentType || 'api::page.page';
308
348
  const dynamicZoneField = pluginConfig.dynamicZoneField || 'sections';
309
349
 
350
+ // Content type'ın kind'ını kontrol et
351
+ const contentTypeModel = strapi.contentTypes[contentType];
352
+ const isSingleType = contentTypeModel?.kind === 'singleType';
353
+
310
354
  const findPage = async (id) => {
355
+ if (isSingleType) {
356
+ // SingleType için direkt findOne kullan
357
+ try {
358
+ return await strapi.entityService.findOne(contentType, {
359
+ populate: [dynamicZoneField],
360
+ });
361
+ } catch (error) {
362
+ return null;
363
+ }
364
+ }
365
+
366
+ // CollectionType için documentId ile bul
311
367
  const numericId = parseInt(id);
312
368
  if (!isNaN(numericId)) {
313
369
  try {
@@ -436,7 +492,23 @@ const controller = ({ strapi }) => ({
436
492
  const contentType = pluginConfig.contentType || 'api::page.page';
437
493
  const dynamicZoneField = pluginConfig.dynamicZoneField || 'sections';
438
494
 
495
+ // Content type'ın kind'ını kontrol et
496
+ const contentTypeModel = strapi.contentTypes[contentType];
497
+ const isSingleType = contentTypeModel?.kind === 'singleType';
498
+
439
499
  const findPage = async (id) => {
500
+ if (isSingleType) {
501
+ // SingleType için direkt findOne kullan
502
+ try {
503
+ return await strapi.entityService.findOne(contentType, {
504
+ populate: [dynamicZoneField],
505
+ });
506
+ } catch (error) {
507
+ return null;
508
+ }
509
+ }
510
+
511
+ // CollectionType için documentId ile bul
440
512
  const numericId = parseInt(id);
441
513
  if (!isNaN(numericId)) {
442
514
  try {
@@ -495,45 +567,18 @@ const controller = ({ strapi }) => ({
495
567
  const pluginConfig = strapi.config.get('plugin::copy-any-component') || {};
496
568
  const contentType = pluginConfig.contentType || 'api::page.page';
497
569
 
498
- const findPage = async (id) => {
499
- const numericId = parseInt(id);
500
- if (!isNaN(numericId)) {
501
- try {
502
- return await strapi.entityService.findOne(contentType, numericId);
503
- } catch (error) {
504
- // Try with documentId
505
- }
506
- }
507
-
508
- try {
509
- const pages = await strapi.entityService.findMany(contentType, {
510
- filters: { documentId: id },
511
- });
512
- return pages[0];
513
- } catch (err) {
514
- return null;
515
- }
516
- };
517
-
518
- const page = await findPage(pageId);
519
-
520
- if (!page) {
521
- ctx.status = 404;
522
- ctx.body = { error: "Page not found: " + pageId, data: null };
523
- return;
524
- }
525
-
526
570
  try {
527
571
  // Strapi 5'te documentService kullanılıyor
572
+ // pageId zaten documentId string'i (frontend'den gönderiliyor)
528
573
  const documentService = strapi.documents(contentType);
529
- const publishedPage = await documentService.publish(page.documentId || page.id);
574
+ const publishedPage = await documentService.publish(pageId);
530
575
 
531
576
  ctx.body = {
532
577
  error: null,
533
578
  data: {
534
579
  pageId: publishedPage.id,
535
580
  documentId: publishedPage.documentId,
536
- pageTitle: publishedPage.title,
581
+ pageTitle: publishedPage.title || publishedPage.attributes?.title,
537
582
  publishedAt: publishedPage.publishedAt,
538
583
  },
539
584
  };