strapi-plugin-map-box 0.0.1 → 0.0.3
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.
- package/README.md +276 -3
- package/dist/admin/index.js +293 -82
- package/dist/admin/index.js.map +1 -1
- package/dist/admin/index.mjs +294 -83
- package/dist/admin/index.mjs.map +1 -1
- package/dist/admin/src/components/custom-field/MapBoxField/MapSearch.d.ts +12 -2
- package/dist/server/index.js +13 -14
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +13 -14
- package/dist/server/index.mjs.map +1 -1
- package/dist/server/src/index.d.ts +1 -1
- package/dist/server/src/routes/index.d.ts +1 -1
- package/dist/server/src/utils/index.d.ts +4 -2
- package/package.json +1 -1
- package/dist/types.d.ts +0 -6
package/README.md
CHANGED
|
@@ -1,4 +1,277 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Strapi Plugin MapBox
|
|
2
2
|
|
|
3
|
-
MapBox map
|
|
4
|
-
|
|
3
|
+
A Strapi plugin that adds a custom MapBox field to your content types. Select locations on an interactive map with search functionality, and store coordinates, zoom level, and address data.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Interactive MapBox map field for Strapi content types
|
|
8
|
+
- Location search with autocomplete
|
|
9
|
+
- Click or drag marker to set location
|
|
10
|
+
- Stores longitude, latitude, zoom, pitch, bearing, and address
|
|
11
|
+
- Debug mode for viewing raw JSON data
|
|
12
|
+
- Fullscreen, navigation, and geolocation controls
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
- Strapi v5.x
|
|
17
|
+
- MapBox account with access token
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
### From npm
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install strapi-plugin-map-box
|
|
25
|
+
# or
|
|
26
|
+
yarn add strapi-plugin-map-box
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### For local development
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Clone the plugin
|
|
33
|
+
git clone https://github.com/PaulBratslavsky/strapi-plugin-map-box.git
|
|
34
|
+
|
|
35
|
+
# Install dependencies
|
|
36
|
+
cd strapi-plugin-map-box
|
|
37
|
+
yarn install
|
|
38
|
+
|
|
39
|
+
# Build the plugin
|
|
40
|
+
yarn build
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Configuration
|
|
44
|
+
|
|
45
|
+
### 1. Add plugin configuration
|
|
46
|
+
|
|
47
|
+
Create or update `config/plugins.ts` in your Strapi project:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
export default () => ({
|
|
51
|
+
'map-box': {
|
|
52
|
+
enabled: true,
|
|
53
|
+
config: {
|
|
54
|
+
public: {
|
|
55
|
+
accessToken: process.env.MAPBOX_ACCESS_TOKEN,
|
|
56
|
+
debugMode: process.env.MAPBOX_DEBUG_MODE === 'true',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. Add environment variables
|
|
64
|
+
|
|
65
|
+
Add to your `.env` file:
|
|
66
|
+
|
|
67
|
+
```env
|
|
68
|
+
MAPBOX_ACCESS_TOKEN=pk.your_mapbox_public_token_here
|
|
69
|
+
MAPBOX_DEBUG_MODE=false
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Get your access token from [MapBox Account](https://account.mapbox.com/access-tokens/).
|
|
73
|
+
|
|
74
|
+
### 3. Update Content Security Policy
|
|
75
|
+
|
|
76
|
+
Update `config/middlewares.ts` to allow MapBox resources:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
export default [
|
|
80
|
+
'strapi::logger',
|
|
81
|
+
'strapi::errors',
|
|
82
|
+
{
|
|
83
|
+
name: 'strapi::security',
|
|
84
|
+
config: {
|
|
85
|
+
contentSecurityPolicy: {
|
|
86
|
+
useDefaults: true,
|
|
87
|
+
directives: {
|
|
88
|
+
'script-src': ["'self'", "'unsafe-inline'", 'blob:'],
|
|
89
|
+
'worker-src': ["'self'", 'blob:'],
|
|
90
|
+
'connect-src': ["'self'", 'https:', 'blob:', 'https://api.mapbox.com', 'https://events.mapbox.com'],
|
|
91
|
+
'img-src': ["'self'", 'data:', 'blob:', 'https://api.mapbox.com', 'https://*.mapbox.com'],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
'strapi::cors',
|
|
97
|
+
'strapi::poweredBy',
|
|
98
|
+
'strapi::query',
|
|
99
|
+
'strapi::body',
|
|
100
|
+
'strapi::session',
|
|
101
|
+
'strapi::favicon',
|
|
102
|
+
'strapi::public',
|
|
103
|
+
];
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 4. Rebuild Strapi
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
yarn build
|
|
110
|
+
yarn develop
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Usage
|
|
114
|
+
|
|
115
|
+
### Adding the field to a content type
|
|
116
|
+
|
|
117
|
+
1. Go to **Content-Type Builder** in Strapi admin
|
|
118
|
+
2. Select or create a content type
|
|
119
|
+
3. Click **Add another field**
|
|
120
|
+
4. Select **Custom** tab
|
|
121
|
+
5. Choose **MapBox** field
|
|
122
|
+
6. Configure field name and settings
|
|
123
|
+
7. Save the content type
|
|
124
|
+
|
|
125
|
+
### Data structure
|
|
126
|
+
|
|
127
|
+
The MapBox field stores the following JSON structure:
|
|
128
|
+
|
|
129
|
+
```json
|
|
130
|
+
{
|
|
131
|
+
"longitude": -122.4194,
|
|
132
|
+
"latitude": 37.7749,
|
|
133
|
+
"zoom": 13,
|
|
134
|
+
"pitch": 0,
|
|
135
|
+
"bearing": 0,
|
|
136
|
+
"address": "San Francisco, CA, USA"
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### API response
|
|
141
|
+
|
|
142
|
+
When fetching content via the API, the map data is returned as a JSON object:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
GET /api/locations
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
"data": [
|
|
151
|
+
{
|
|
152
|
+
"id": 1,
|
|
153
|
+
"documentId": "abc123",
|
|
154
|
+
"mapData": {
|
|
155
|
+
"longitude": -122.4194,
|
|
156
|
+
"latitude": 37.7749,
|
|
157
|
+
"zoom": 13,
|
|
158
|
+
"pitch": 0,
|
|
159
|
+
"bearing": 0,
|
|
160
|
+
"address": "San Francisco, CA, USA"
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Local Development
|
|
168
|
+
|
|
169
|
+
### Link to a Strapi project
|
|
170
|
+
|
|
171
|
+
Using the resolve path in your Strapi project's `config/plugins.ts`:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
export default () => ({
|
|
175
|
+
'map-box': {
|
|
176
|
+
enabled: true,
|
|
177
|
+
resolve: '../path-to/strapi-plugin-map-box',
|
|
178
|
+
config: {
|
|
179
|
+
public: {
|
|
180
|
+
accessToken: process.env.MAPBOX_ACCESS_TOKEN,
|
|
181
|
+
debugMode: true,
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Watch mode
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
yarn watch
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Frontend Integration
|
|
195
|
+
|
|
196
|
+
### React / Next.js
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
npm install react-map-gl mapbox-gl
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
import Map, { Marker } from 'react-map-gl/mapbox';
|
|
204
|
+
import 'mapbox-gl/dist/mapbox-gl.css';
|
|
205
|
+
|
|
206
|
+
function LocationMap({ location }) {
|
|
207
|
+
return (
|
|
208
|
+
<Map
|
|
209
|
+
initialViewState={{
|
|
210
|
+
longitude: location.longitude,
|
|
211
|
+
latitude: location.latitude,
|
|
212
|
+
zoom: location.zoom,
|
|
213
|
+
pitch: location.pitch,
|
|
214
|
+
bearing: location.bearing,
|
|
215
|
+
}}
|
|
216
|
+
mapStyle="mapbox://styles/mapbox/streets-v12"
|
|
217
|
+
mapboxAccessToken={process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN}
|
|
218
|
+
>
|
|
219
|
+
<Marker
|
|
220
|
+
longitude={location.longitude}
|
|
221
|
+
latitude={location.latitude}
|
|
222
|
+
color="#4945ff"
|
|
223
|
+
/>
|
|
224
|
+
</Map>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### React Native (Expo)
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
npx expo install @rnmapbox/maps
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
import Mapbox, { Camera, MapView, PointAnnotation } from '@rnmapbox/maps';
|
|
237
|
+
|
|
238
|
+
Mapbox.setAccessToken(process.env.EXPO_PUBLIC_MAPBOX_ACCESS_TOKEN);
|
|
239
|
+
|
|
240
|
+
function LocationMap({ location }) {
|
|
241
|
+
return (
|
|
242
|
+
<MapView style={{ flex: 1 }}>
|
|
243
|
+
<Camera
|
|
244
|
+
centerCoordinate={[location.longitude, location.latitude]}
|
|
245
|
+
zoomLevel={location.zoom}
|
|
246
|
+
pitch={location.pitch}
|
|
247
|
+
heading={location.bearing}
|
|
248
|
+
/>
|
|
249
|
+
<PointAnnotation
|
|
250
|
+
id="marker"
|
|
251
|
+
coordinate={[location.longitude, location.latitude]}
|
|
252
|
+
/>
|
|
253
|
+
</MapView>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## TypeScript
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
type MapLocation = {
|
|
262
|
+
longitude: number;
|
|
263
|
+
latitude: number;
|
|
264
|
+
zoom: number;
|
|
265
|
+
pitch: number;
|
|
266
|
+
bearing: number;
|
|
267
|
+
address?: string;
|
|
268
|
+
};
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT
|
|
274
|
+
|
|
275
|
+
## Author
|
|
276
|
+
|
|
277
|
+
Paul Bratslavsky ([@PaulBratslavsky](https://github.com/PaulBratslavsky))
|