Files
WooNooW/admin-spa/src/lib/wp-media.ts
dwindown 493f363dd2 feat: WordPress Media Modal Integration! 🎉
##  Improvements 4-5 Complete - Respecting WordPress!

### 4. WordPress Media Modal for TipTap Images
**Before:**
- Prompt dialog for image URL
- Manual URL entry
- No media library access

**After:**
- Native WordPress Media Modal
- Browse existing uploads
- Upload new images
- Full media library features
- Alt text, dimensions included

**Implementation:**
- `wp-media.ts` helper library
- `openWPMediaImage()` function
- Integrates with TipTap Image extension
- Sets src, alt, title automatically

### 5. WordPress Media Modal for Store Logos/Favicon
**Before:**
- Only drag-and-drop or file picker
- No access to existing media

**After:**
- "Choose from Media Library" button
- Filtered by media type:
  - Logo: PNG, JPEG, SVG, WebP
  - Favicon: PNG, ICO
- Browse and reuse existing assets
- Professional WordPress experience

**Implementation:**
- Updated `ImageUpload` component
- Added `mediaType` prop
- Three specialized functions:
  - `openWPMediaLogo()`
  - `openWPMediaFavicon()`
  - `openWPMediaImage()`

## 📦 New Files:

**lib/wp-media.ts:**
```typescript
- openWPMedia() - Core function
- openWPMediaImage() - For general images
- openWPMediaLogo() - For logos (filtered)
- openWPMediaFavicon() - For favicons (filtered)
- WPMediaFile interface
- Full TypeScript support
```

## 🎨 User Experience:

**Email Builder:**
- Click image icon in RichTextEditor
- WordPress Media Modal opens
- Select from library or upload
- Image inserted with proper attributes

**Store Settings:**
- Drag-and-drop still works
- OR click "Choose from Media Library"
- Filtered by appropriate file types
- Reuse existing brand assets

## 🙏 Respect to WordPress:

**Why This Matters:**
1. **Familiar Interface** - Users know WordPress Media
2. **Existing Assets** - Access uploaded media
3. **Better UX** - No manual URL entry
4. **Professional** - Native WordPress integration
5. **Consistent** - Same as Posts/Pages

**WordPress Integration:**
- Uses `window.wp.media` API
- Respects user permissions
- Works with media library
- Proper nonce handling
- Full compatibility

## 📋 All 5 Improvements Complete:

 1. Heading Selector (H1-H4, Paragraph)
 2. Styled Buttons in Cards (matching standalone)
 3. Variable Pills for Button Links
 4. WordPress Media for TipTap Images
 5. WordPress Media for Store Logos/Favicon

## 🚀 Ready for Production!

All user feedback implemented perfectly! 🎉
2025-11-13 09:48:47 +07:00

162 lines
3.5 KiB
TypeScript

/**
* WordPress Media Library Integration
*
* Provides a clean interface to WordPress's native media modal.
* Respects WordPress conventions and user familiarity.
*/
declare global {
interface Window {
wp: {
media: (options: any) => {
on: (event: string, callback: (...args: any[]) => void) => void;
open: () => void;
state: () => {
get: (key: string) => {
first: () => {
toJSON: () => {
url: string;
id: number;
title: string;
filename: string;
alt: string;
width: number;
height: number;
};
};
};
};
};
};
}
}
export interface WPMediaFile {
url: string;
id: number;
title: string;
filename: string;
alt?: string;
width?: number;
height?: number;
}
export interface WPMediaOptions {
title?: string;
button?: {
text: string;
};
multiple?: boolean;
library?: {
type?: string | string[];
};
}
/**
* Open WordPress Media Modal
*
* @param options - Configuration for the media modal
* @param onSelect - Callback when media is selected
* @returns Promise that resolves when modal is closed
*/
export function openWPMedia(
options: WPMediaOptions = {},
onSelect: (file: WPMediaFile) => void
): void {
// Check if WordPress media is available
if (typeof window.wp === 'undefined' || typeof window.wp.media === 'undefined') {
console.error('WordPress media library is not available');
alert('WordPress media library is not loaded. Please refresh the page.');
return;
}
// Default options
const defaultOptions: WPMediaOptions = {
title: 'Select or Upload Media',
button: {
text: 'Use this media',
},
multiple: false,
};
// Merge options
const modalOptions = { ...defaultOptions, ...options };
// Create media frame
const frame = window.wp.media(modalOptions);
// Handle selection
frame.on('select', () => {
const attachment = frame.state().get('selection').first().toJSON();
const file: WPMediaFile = {
url: attachment.url,
id: attachment.id,
title: attachment.title || attachment.filename,
filename: attachment.filename,
alt: attachment.alt || '',
width: attachment.width,
height: attachment.height,
};
onSelect(file);
});
// Open modal
frame.open();
}
/**
* Open WordPress Media Modal for Images Only
*/
export function openWPMediaImage(onSelect: (file: WPMediaFile) => void): void {
openWPMedia(
{
title: 'Select or Upload Image',
button: {
text: 'Use this image',
},
library: {
type: 'image',
},
},
onSelect
);
}
/**
* Open WordPress Media Modal for Logo/Icon
*/
export function openWPMediaLogo(onSelect: (file: WPMediaFile) => void): void {
openWPMedia(
{
title: 'Select or Upload Logo',
button: {
text: 'Use this logo',
},
library: {
type: ['image/png', 'image/jpeg', 'image/svg+xml', 'image/webp'],
},
},
onSelect
);
}
/**
* Open WordPress Media Modal for Favicon
*/
export function openWPMediaFavicon(onSelect: (file: WPMediaFile) => void): void {
openWPMedia(
{
title: 'Select or Upload Favicon',
button: {
text: 'Use this favicon',
},
library: {
type: ['image/png', 'image/x-icon', 'image/vnd.microsoft.icon'],
},
},
onSelect
);
}