@silexlabs/silex 3.0.0-alpha.151 → 3.0.0-alpha.153

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 (29) hide show
  1. package/dist/client/client/grapesjs/index.d.ts +4 -0
  2. package/dist/client/client/grapesjs/keymaps.d.ts +71 -0
  3. package/dist/client/client/grapesjs/project-bar.d.ts +4 -0
  4. package/dist/client/client/utils.d.ts +7 -0
  5. package/dist/client/css/admin.css +176 -6
  6. package/dist/client/js/main.js +118 -109
  7. package/dist/plugins/server/plugins/server/GitlabConnector.d.ts +40 -9
  8. package/dist/plugins/server/plugins/server/GitlabConnector.js +58 -34
  9. package/dist/plugins/server/plugins/server/GitlabConnector.js.map +1 -1
  10. package/dist/plugins/server/plugins/server/GitlabHostingConnector.d.ts +18 -0
  11. package/dist/plugins/server/plugins/server/GitlabHostingConnector.js +98 -0
  12. package/dist/plugins/server/plugins/server/GitlabHostingConnector.js.map +1 -0
  13. package/package.json +6 -5
  14. package/src/css/grapesjs-plugins.scss +160 -0
  15. package/src/css/grapesjs.scss +31 -0
  16. package/src/css/project-bar.scss +5 -7
  17. package/src/ts/client/api.ts +8 -1
  18. package/src/ts/client/config.ts +1 -1
  19. package/src/ts/client/grapesjs/PublicationManager.ts +5 -0
  20. package/src/ts/client/grapesjs/PublicationUi.ts +45 -27
  21. package/src/ts/client/grapesjs/css-props.ts +608 -257
  22. package/src/ts/client/grapesjs/index.ts +91 -18
  23. package/src/ts/client/grapesjs/internal-links.ts +11 -1
  24. package/src/ts/client/grapesjs/keymaps.ts +155 -0
  25. package/src/ts/client/grapesjs/project-bar.ts +18 -7
  26. package/src/ts/client/grapesjs/settings.ts +44 -20
  27. package/src/ts/client/utils.ts +33 -9
  28. package/src/ts/plugins/server/GitlabConnector.ts +70 -46
  29. package/src/ts/plugins/server/GitlabHostingConnector.ts +102 -0
@@ -18,7 +18,7 @@
18
18
  import { API_CONNECTOR_LOGIN_CALLBACK, API_CONNECTOR_PATH, API_PATH, WEBSITE_DATA_FILE } from '../../constants'
19
19
  import { ServerConfig } from '../../server/config'
20
20
  import { ConnectorFile, ConnectorFileContent, StatusCallback, StorageConnector, contentToBuffer, contentToString, toConnectorData } from '../../server/connectors/connectors'
21
- import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, WebsiteMeta, WebsiteMetaFileContent } from '../../types'
21
+ import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, WebsiteMeta, WebsiteMetaFileContent, JobStatus } from '../../types'
22
22
  import fetch from 'node-fetch'
23
23
  import crypto, { createHash } from 'crypto'
24
24
  import { join } from 'path'
@@ -38,12 +38,13 @@ export interface GitlabOptions {
38
38
  //metaRepoFile: string
39
39
  repoPrefix: string
40
40
  scope: string
41
+ domain: string
41
42
  }
42
43
 
43
- interface GitlabToken {
44
- state: string
45
- codeVerifier: string
46
- codeChallenge: string
44
+ export interface GitlabToken {
45
+ state?: string
46
+ codeVerifier?: string
47
+ codeChallenge?: string
47
48
  token?: {
48
49
  access_token: string
49
50
  token_type: string
@@ -57,9 +58,7 @@ interface GitlabToken {
57
58
  username?: string
58
59
  }
59
60
 
60
- interface GitlabSession {
61
- gitlab?: GitlabToken
62
- }
61
+ export type GitlabSession = Record<string, GitlabToken>
63
62
 
64
63
  interface GitlabAction {
65
64
  action: 'create' | 'delete' | 'move' | 'update'
@@ -105,10 +104,6 @@ interface MetaRepoFileContent {
105
104
  }
106
105
  }
107
106
 
108
- async function wait(ms: number) {
109
- return new Promise(resolve => setTimeout(resolve, ms))
110
- }
111
-
112
107
  const svg = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
113
108
  <svg
114
109
  xmlns:svg="http://www.w3.org/2000/svg"
@@ -189,6 +184,8 @@ export default class GitlabConnector implements StorageConnector {
189
184
  } as GitlabOptions
190
185
  if(!this.options.clientId) throw new Error('Missing Gitlab client ID')
191
186
  if(!this.options.clientSecret) throw new Error('Missing Gitlab client secret')
187
+ if(!this.options.domain) throw new Error('Missing Gitlab domain')
188
+
192
189
  }
193
190
 
194
191
  // **
@@ -197,26 +194,28 @@ export default class GitlabConnector implements StorageConnector {
197
194
  return encodeURIComponent(join(this.options.assetsFolder, path))
198
195
  }
199
196
 
200
- private async createFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
197
+ async createFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
201
198
  // Remove leading slash
202
199
  const safePath = path.replace(/^\//, '')
200
+ const encodePath = decodeURIComponent(path)
203
201
  return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'POST', {
204
202
  id: websiteId,
205
203
  branch: this.options.branch,
206
204
  content,
207
- commit_message: `Create file ${path} from Silex`,
205
+ commit_message: `Create file ${encodePath} from Silex`,
208
206
  encoding: isBase64 ? 'base64' : undefined,
209
207
  })
210
208
  }
211
209
 
212
- private async updateFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
210
+ async updateFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
213
211
  // Remove leading slash
214
212
  const safePath = path.replace(/^\//, '')
213
+ const encodePath = decodeURIComponent(path)
215
214
  return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'PUT', {
216
215
  id: websiteId,
217
216
  branch: this.options.branch,
218
217
  content: await contentToString(content),
219
- commit_message: `Update website asset ${path} from Silex`,
218
+ commit_message: `Update website asset ${encodePath} from Silex`,
220
219
  encoding: isBase64 ? 'base64' : undefined,
221
220
  })
222
221
  }
@@ -224,7 +223,7 @@ export default class GitlabConnector implements StorageConnector {
224
223
  async readFile(session: GitlabSession, websiteId: string, fileName: string): Promise<Buffer> {
225
224
  const safePath = fileName.replace(/^\//, '')
226
225
  // Call the API
227
- const url = `https://gitlab.com/api/v4/projects/${websiteId}/repository/files/${safePath}?ref=${this.options.branch}&access_token=${session.gitlab?.token?.access_token}`
226
+ const url = `${this.options.domain}/api/v4/projects/${websiteId}/repository/files/${safePath}?ref=${this.options.branch}&access_token=${this.getSessionToken(session).token?.access_token}`
228
227
  const response = await fetch(url, {
229
228
  method: 'GET',
230
229
  headers: {
@@ -244,8 +243,8 @@ export default class GitlabConnector implements StorageConnector {
244
243
  * The meta repo contains a JSON file which contains the list of websites
245
244
  */
246
245
  //private getMetaRepoPath(session: GitlabSession): string {
247
- // if(!session.gitlab?.username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
248
- // return encodeURIComponent(`${session.gitlab.username}/${this.options.metaRepo}`)
246
+ // if(!this.getSessionToken(session).username) throw new ApiError('Missing Gitlab user ID. User not logged in?', 401)
247
+ // return encodeURIComponent(`${this.getSessionToken(session).username}/${this.options.metaRepo}`)
249
248
  //}
250
249
 
251
250
  ///**
@@ -269,11 +268,11 @@ export default class GitlabConnector implements StorageConnector {
269
268
  /**
270
269
  * Call the Gitlab API with the user's token and handle errors
271
270
  */
272
- private async callApi(session: GitlabSession, path: string, method: 'POST' | 'GET' | 'PUT' | 'DELETE' = 'GET', body: GitlabWriteFile | GitlabGetToken | GitlabWebsiteName | GitlabCreateBranch | null = null, params: any = {}): Promise<any> {
273
- const token = session?.gitlab?.token
271
+ async callApi(session: GitlabSession, path: string, method: 'POST' | 'GET' | 'PUT' | 'DELETE' = 'GET', body: GitlabWriteFile | GitlabGetToken | GitlabWebsiteName | GitlabCreateBranch | null = null, params: any = {}): Promise<any> {
272
+ const token = this.getSessionToken(session).token
274
273
  const tokenParam = token ? `access_token=${token.access_token}&` : ''
275
274
  const paramsStr = Object.entries(params).map(([k, v]) => `${k}=${encodeURIComponent((v as any).toString())}`).join('&')
276
- const url = `https://gitlab.com/${path}?${tokenParam}${paramsStr}`
275
+ const url = `${this.options.domain}/${path}?${tokenParam}${paramsStr}`
277
276
  const headers = {
278
277
  'Content-Type': 'application/json',
279
278
  }
@@ -295,16 +294,16 @@ export default class GitlabConnector implements StorageConnector {
295
294
  if(!response.ok) {
296
295
  if (text.includes('A file with this name doesn\'t exist')) {
297
296
  throw new ApiError('Gitlab API error (5): Not Found', 404)
298
- } else if (response.status === 401 && session?.gitlab?.token?.refresh_token) {
297
+ } else if (response.status === 401 && this.getSessionToken(session).token?.refresh_token) {
299
298
  // Refresh the token
300
- const token = session?.gitlab?.token
299
+ const token = this.getSessionToken(session).token
301
300
  const body = {
302
301
  grant_type: 'refresh_token',
303
- refresh_token: token.refresh_token,
302
+ refresh_token: token?.refresh_token,
304
303
  client_id: this.options.clientId,
305
304
  client_secret: this.options.clientSecret,
306
305
  }
307
- const response = await fetch('https://gitlab.com/oauth/token', {
306
+ const response = await fetch(this.options.domain + '/oauth/token', {
308
307
  method: 'POST',
309
308
  headers: {
310
309
  'Content-Type': 'application/json',
@@ -313,14 +312,16 @@ export default class GitlabConnector implements StorageConnector {
313
312
  })
314
313
  const refreshJson = await response.json()
315
314
  if (response.ok) {
316
- session.gitlab.token = {
317
- ...token,
318
- ...refreshJson,
319
- }
315
+ this.setSessionToken(session, {
316
+ token: {
317
+ ...token,
318
+ ...refreshJson,
319
+ },
320
+ } as GitlabToken)
320
321
  return await this.callApi(session, path, method, body as any, params)
321
322
  } else {
322
323
  const message = typeof refreshJson?.message === 'object' ? Object.entries(refreshJson.message).map(entry => entry.join(' ')).join(' ') : refreshJson?.message ?? refreshJson?.error ?? response.statusText
323
- console.error('Gitlab API error (2) - could not refresh token', response.status, response.statusText, {message}, 'refresh_token:', token.refresh_token)
324
+ console.error('Gitlab API error (2) - could not refresh token', response.status, response.statusText, {message}, 'refresh_token:', token?.refresh_token)
324
325
  // Workaround for when the token is invalid
325
326
  // It happens often which is not normal (refresh token should last 6 months)
326
327
  this.logout(session)
@@ -329,6 +330,7 @@ export default class GitlabConnector implements StorageConnector {
329
330
  }
330
331
  } else {
331
332
  const message = typeof json?.message === 'object' ? Object.entries(json.message).map(entry => entry.join(' ')).join(' ') : json?.message ?? json?.error ?? response.statusText
333
+ console.error('Gitlab API error (1)', response.status, response.statusText, {url, method, body, params, text, message})
332
334
  throw new ApiError(`Gitlab API error (1): ${message}`, response.status)
333
335
  }
334
336
  }
@@ -385,13 +387,23 @@ export default class GitlabConnector implements StorageConnector {
385
387
  const codeChallenge = await this.generateCodeChallenge(codeVerifier)
386
388
 
387
389
  // Store the code verifier and code challenge in the session
388
- session.gitlab = {
389
- ...session.gitlab,
390
+ this.setSessionToken(session, {
391
+ ...this.getSessionToken(session),
390
392
  state,
391
393
  codeVerifier,
392
394
  codeChallenge,
393
- }
394
- return `https://gitlab.com/oauth/authorize?client_id=${this.options.clientId}&redirect_uri=${redirect_uri}&response_type=code&state=${session.gitlab.state}&scope=${this.options.scope}&code_challenge=${codeChallenge}&code_challenge_method=S256`
395
+ })
396
+ return `${this.options.domain}/oauth/authorize?client_id=${this.options.clientId}&redirect_uri=${redirect_uri}&response_type=code&state=${this.getSessionToken(session).state}&scope=${this.options.scope}&code_challenge=${codeChallenge}&code_challenge_method=S256`
397
+ }
398
+
399
+ getSessionToken(session: GitlabSession | undefined): GitlabToken {
400
+ return (session ?? {})[this.connectorId] ?? {}
401
+ }
402
+ setSessionToken(session: GitlabSession, token: GitlabToken): void {
403
+ session[this.connectorId] = token
404
+ }
405
+ resetSessionToken(session: GitlabSession): void {
406
+ delete session[this.connectorId]
395
407
  }
396
408
 
397
409
  getOptions(formData: object): object {
@@ -407,7 +419,7 @@ export default class GitlabConnector implements StorageConnector {
407
419
  }
408
420
 
409
421
  async isLoggedIn(session: GitlabSession): Promise<boolean> {
410
- return !!session?.gitlab?.token
422
+ return !!this.getSessionToken(session).token
411
423
  }
412
424
 
413
425
  /**
@@ -416,11 +428,12 @@ export default class GitlabConnector implements StorageConnector {
416
428
  * OAuth2 Step #2 from https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-with-proof-key-for-code-exchange-pkce
417
429
  */
418
430
  async setToken(session: GitlabSession, loginResult: any): Promise<void> {
419
- if(!loginResult.state || loginResult.state !== session.gitlab?.state) throw new ApiError('Invalid state', 401)
420
- if(!session.gitlab?.codeVerifier) throw new ApiError('Missing code verifier', 401)
421
- if(!session.gitlab?.codeChallenge) throw new ApiError('Missing code challenge', 401)
431
+ const sessionToken = this.getSessionToken(session)
432
+ if(!loginResult.state || loginResult.state !== sessionToken?.state) throw new ApiError('Invalid state', 401)
433
+ if(!sessionToken?.codeVerifier) throw new ApiError('Missing code verifier', 401)
434
+ if(!sessionToken?.codeChallenge) throw new ApiError('Missing code challenge', 401)
422
435
 
423
- const response = await fetch('https://gitlab.com/oauth/token', {
436
+ const response = await fetch(this.options.domain + '/oauth/token', {
424
437
  method: 'POST',
425
438
  headers: {
426
439
  'Content-Type': 'application/json',
@@ -431,23 +444,31 @@ export default class GitlabConnector implements StorageConnector {
431
444
  code: loginResult.code,
432
445
  grant_type: 'authorization_code',
433
446
  redirect_uri: this.getRedirect(),
434
- code_verifier: session.gitlab.codeVerifier,
447
+ code_verifier: sessionToken.codeVerifier,
435
448
  }),
436
449
  })
437
450
 
438
451
  const token = await response.json()
439
452
 
440
453
  // Store the token in the session
441
- session.gitlab = { token, state: session.gitlab.state, codeVerifier: session.gitlab.codeVerifier, codeChallenge: session.gitlab.codeChallenge }
454
+ this.setSessionToken(session, {
455
+ token,
456
+ ...this.getSessionToken(session),
457
+ })
442
458
 
443
459
  // We need to get the user ID for listWebsites
444
460
  const user = await this.callApi(session, 'api/v4/user') as any
445
- session.gitlab.userId = user.id
446
- session.gitlab.username = user.username
461
+
462
+ // Store the user details in the session
463
+ this.setSessionToken(session, {
464
+ ...this.getSessionToken(session),
465
+ userId: user.id,
466
+ username: user.username,
467
+ })
447
468
  }
448
469
 
449
470
  async logout(session: GitlabSession): Promise<void> {
450
- delete session.gitlab
471
+ this.resetSessionToken(session)
451
472
  }
452
473
 
453
474
  async getUser(session: GitlabSession): Promise<ConnectorUser> {
@@ -483,7 +504,7 @@ export default class GitlabConnector implements StorageConnector {
483
504
  // throw e
484
505
  // }
485
506
  //}
486
- const projects = await this.callApi(session, `api/v4/users/${session.gitlab?.userId}/projects`) as any[]
507
+ const projects = await this.callApi(session, `api/v4/users/${this.getSessionToken(session).userId}/projects`) as any[]
487
508
  return projects
488
509
  .filter(p => p.name.startsWith(this.options.repoPrefix))
489
510
  .map(p => ({
@@ -644,6 +665,7 @@ export default class GitlabConnector implements StorageConnector {
644
665
  }
645
666
 
646
667
  async writeAssets(session: GitlabSession, websiteId: string, files: ConnectorFile[], status?: StatusCallback | undefined): Promise<void> {
668
+ status && await status({ message: 'in progress...', status: JobStatus.IN_PROGRESS })
647
669
  // For each file
648
670
  for (const file of files) {
649
671
  // Convert to base64
@@ -657,10 +679,12 @@ export default class GitlabConnector implements StorageConnector {
657
679
  if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
658
680
  await this.createFile(session, websiteId, path, content, true)
659
681
  } else {
682
+ status && await status({ message: 'Error', status: JobStatus.ERROR })
660
683
  throw e
661
684
  }
662
685
  }
663
686
  }
687
+ status && await status({ message: 'Successfull', status: JobStatus.SUCCESS })
664
688
  }
665
689
 
666
690
  async readAsset(session: GitlabSession, websiteId: string, fileName: string): Promise<ConnectorFileContent> {
@@ -0,0 +1,102 @@
1
+ /*
2
+ * Silex website builder, free/libre no-code tool for makers.
3
+ * Copyright (c) 2023 lexoyo and Silex Labs foundation
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ * Gitlab hosting connector
20
+ * @fileoverview Gitlab hosting connector for Silex, connect to the user's Gitlab account to host websites with .gitlab-ci.yml file template for plain html by default
21
+ * @see https://docs.gitlab.com/ee/api/oauth2.html
22
+ * @see https://docs.gitlab.com/ee/user/project/pages/getting_started/pages_ci_cd_template.html
23
+ */
24
+
25
+ import GitlabConnector, { GitlabOptions, GitlabSession} from './GitlabConnector'
26
+ import { HostingConnector, ConnectorFile } from '../../server/connectors/connectors'
27
+ import { ConnectorType, WebsiteId, JobData, JobStatus, PublicationJobData } from '../../types'
28
+ import { JobManager } from '../../server/jobs'
29
+ import { join } from 'path'
30
+ import { ServerConfig } from '../../server/config'
31
+
32
+ export default class GitlabHostingConnector extends GitlabConnector implements HostingConnector {
33
+
34
+ displayName = 'Gitlab hosting'
35
+ connectorType = ConnectorType.HOSTING
36
+
37
+ constructor( config: ServerConfig, opts: Partial<GitlabOptions>) {
38
+ super (config, opts)
39
+ /* public directory for standard gitlab pages */
40
+ this.options.assetsFolder= 'public'
41
+ }
42
+
43
+ async publish(session: GitlabSession, websiteId: WebsiteId, files: ConnectorFile[], {startJob, jobSuccess, jobError}: JobManager): Promise<JobData> {
44
+ const job = startJob(`Publishing to ${this.displayName}`) as PublicationJobData
45
+ job.logs = [[`Publishing to ${this.displayName}`]]
46
+ job.errors = [[]]
47
+ /* Configuration file .gitlab-ci.yml contains template for plain html Gitlab pages*/
48
+ const pathYml = '.gitlab-ci.yml'
49
+ const contentYml =
50
+ `# This file is a template, and might need editing before it works on your project.
51
+ # To contribute improvements to CI/CD templates, please follow the Development guide at:
52
+ # https://docs.gitlab.com/ee/development/cicd/templates.html
53
+ # This specific template is located at:
54
+ # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Pages/HTML.gitlab-ci.yml
55
+
56
+ # Full project: https://gitlab.com/pages/plain-html
57
+ image: busybox
58
+
59
+ pages:
60
+ stage: deploy
61
+ environment: production
62
+ script:
63
+ - echo "The site will be deployed to $CI_PAGES_URL"
64
+ artifacts:
65
+ paths:
66
+ - public
67
+ rules:
68
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH`
69
+ try {
70
+ await this.readFile(session, websiteId, pathYml)
71
+ } catch (e) {
72
+ // If the file .gitlab-ci.yml does not exist, create it, otherwise do nothing (do not overwriting existing one)
73
+ if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
74
+ await this.createFile(session, websiteId, pathYml, contentYml)
75
+ }
76
+ }
77
+
78
+ /* publishing all files for website*/
79
+ await this.writeAssets(session, websiteId, files, async ({status, message}) => {
80
+ // Update the job status
81
+ job.status = status
82
+ job.message = message
83
+ job.logs[0].push(message)
84
+ if(status === JobStatus.SUCCESS) {
85
+ const gitlabUrl = await this.getUrl(session, websiteId)
86
+ job.message = gitlabUrl
87
+ job.logs[0].push(gitlabUrl)
88
+ jobSuccess(job.jobId, 'Gitlab pages PUBLISHED: ' + '<a href="' + gitlabUrl + '" target="_blank">' + gitlabUrl + '</a>')
89
+ } else if(status === JobStatus.ERROR) {
90
+ job.errors[0].push(message)
91
+ jobError(job.jobId, message)
92
+ }
93
+ })
94
+ return job
95
+ }
96
+
97
+ /* Get and return Url Gitlab Pages */
98
+ async getUrl(session: GitlabSession, websiteId: WebsiteId): Promise<string> {
99
+ const response = await this.callApi(session, `api/v4/projects/${websiteId}/pages`, 'GET')
100
+ return response.url
101
+ }
102
+ }