Skip to content

Commit

Permalink
Add CanvasSpot docs
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Aug 30, 2023
1 parent e5c691f commit b495cfc
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 34 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = {
['/api/i18n', 'I18n'],
['/api/canvas', 'Canvas'],
['/api/frame', `${subDivider}Frame`],
['/api/canvas_spot', `${subDivider}CanvasSpot`],
['/api/assets', 'Asset Manager'],
['/api/asset', `${subDivider}Asset`],
['/api/block_manager', 'Block Manager'],
Expand Down
7 changes: 6 additions & 1 deletion docs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async function generateDocs () {
['undo_manager/index.ts', 'undo_manager.md'],
['canvas/index.ts', 'canvas.md'],
['canvas/model/Frame.ts', 'frame.md'],
['canvas/model/CanvasSpot.ts', 'canvas_spot.md'],
['i18n/index.ts', 'i18n.md'],
['navigator/index.ts', 'layer_manager.md'],
['pages/index.ts', 'pages.md'],
Expand All @@ -83,6 +84,7 @@ async function generateDocs () {
return documentation.build([filePath], { shallow: true })
.then(cm => documentation.formats.md(cm, /*{ markdownToc: true }*/))
.then(async (output) => {
let addLogs = [];
let result = output
.replace(/\*\*\\\[/g, '**[')
.replace(/\*\*\(\\\[/g, '**([')
Expand All @@ -97,11 +99,14 @@ async function generateDocs () {
// Search for module event documentation
if (result.indexOf(REPLACE_EVENTS) >= 0) {
const eventsMd = await getEventsMdFromTypes(filePath);
if (eventsMd && result.indexOf(REPLACE_EVENTS) >= 0) {
addLogs.push('replaced events');
}
result = eventsMd ? result.replace(REPLACE_EVENTS, `## Available Events\n${eventsMd}`) : result;
}

fs.writeFileSync(`${docRoot}/api/${file[1]}`, result);
log('Created', file[1]);
log('Created', file[1], addLogs.length ? `(${addLogs.join(', ')})` : '');
});
}));

Expand Down
32 changes: 0 additions & 32 deletions docs/api/canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,38 +245,6 @@ const coords = canvas.getCoords();

Returns **[Object][2]** Object containing coordinates

## addFrame

Add new frame to the canvas

### Parameters

* `props` **[Object][2]** Frame properties (optional, default `{}`)
* `opts` (optional, default `{}`)

### Examples

```javascript
canvas.addFrame({
name: 'Mobile home page',
x: 100, // Position in canvas
y: 100,
width: 500, // Frame dimensions
height: 600,
// device: 'DEVICE-ID',
components: [
'<h1 class="testh">Title frame</h1>',
'<p class="testp">Paragraph frame</p>',
],
styles: `
.testh { color: red; }
.testp { color: blue; }
`,
});
```

Returns **[Frame]**

## getLastDragResult

Get the last created Component from a drag & drop to the canvas.
Expand Down
78 changes: 78 additions & 0 deletions docs/api/canvas_spot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

## CanvasSpot



Canvas spots are elements drawn on top of the canvas. They can be used to represent anything you
might need but the most common use case of canvas spots is rendering information and managing
components rendered in the canvas.
Read here for more information about [Canvas Spots][1]

[Component]: component.html

### Properties

* `id` **[String][2]** Spot ID.
* `type` **[String][2]** Spot type.
* `component` **[Component]?** Component to which the spot will be attached.
* `componentView` **ComponentView?** ComponentView to which the spot will be attached.
* `boxRect` **[Object][3]?** Fixed box rect of the spot, eg. `{ width: 100, height: 100, x: 0, y: 0 }`.

### getBoxRect

Get the box rect of the spot.

#### Parameters

* `opts` **[Object][3]** (optional, default `{}`)

#### Examples

```javascript
canvasSpot.getBoxRect();
// { width: 100, height: 50, x: 0, y: 0 }
```

Returns **[Object][3]** The box rect object

### getStyle

Get the style object of the spot.

#### Parameters

* `opts` **[Object][3]** (optional, default `{}`)

#### Examples

```javascript
canvasSpot.getStyle();
// { width: '100px', height: '...', ... }
```

Returns **CSSStyleDeclaration** \[opts]

### isType

Check the spot type.

#### Parameters

* `type` **[String][2]**

#### Examples

```javascript
canvasSpot.isType('select');
```

Returns **[Boolean][4]**

[1]: https://github.com/GrapesJS/grapesjs/blob/dev/docs/modules/Canvas.md#canvas-spots

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
54 changes: 53 additions & 1 deletion src/canvas/model/CanvasSpot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,46 @@ export type CanvasSpotType = LiteralUnion<CanvasSpotBuiltInType, string>;

/** @private */
export interface CanvasSpotBase<T extends CanvasSpotType> {
/**
* Spot type, eg. `select`.
*/
type: T;
/**
* Spot ID.
*/
id: string;
/**
* Fixed box rect of the spot, eg. `{ width: 100, height: 100, x: 0, y: 0 }`.
*/
boxRect?: BoxRect;
/**
* Component to which the spot will be attached.
*/
component?: Component;
/**
* ComponentView to which the spot will be attached.
*/
componentView?: ComponentView;
frame?: Frame;
}

/** @private */
export interface CanvasSpotProps<T extends CanvasSpotType = CanvasSpotType> extends CanvasSpotBase<T> {}

/**
* Canvas spots are elements drawn on top of the canvas. They can be used to represent anything you
* might need but the most common use case of canvas spots is rendering information and managing
* components rendered in the canvas.
* Read here for more information about [Canvas Spots](https://github.com/GrapesJS/grapesjs/blob/dev/docs/modules/Canvas.md#canvas-spots)
*
* [Component]: component.html
*
* @property {String} id Spot ID.
* @property {String} type Spot type.
* @property {[Component]} [component] Component to which the spot will be attached.
* @property {ComponentView} [componentView] ComponentView to which the spot will be attached.
* @property {Object} [boxRect] Fixed box rect of the spot, eg. `{ width: 100, height: 100, x: 0, y: 0 }`.
*
*/
export default class CanvasSpot<T extends CanvasSpotProps = CanvasSpotProps> extends ModuleModel<CanvasModule, T> {
defaults() {
return {
Expand All @@ -57,6 +86,14 @@ export default class CanvasSpot<T extends CanvasSpotProps = CanvasSpotProps> ext
return this.componentView?.el;
}

/**
* Get the box rect of the spot.
* @param {Object} [opts={}]
* @returns {Object} The box rect object
* @example
* canvasSpot.getBoxRect();
* // { width: 100, height: 50, x: 0, y: 0 }
*/
getBoxRect(opts?: GetBoxRectOptions) {
const { el, em } = this;
const cvView = em.Canvas.getCanvasView();
Expand All @@ -76,6 +113,14 @@ export default class CanvasSpot<T extends CanvasSpotProps = CanvasSpotProps> ext
};
}

/**
* Get the style object of the spot.
* @param {Object} [opts={}]
* @returns {CSSStyleDeclaration} [opts]
* @example
* canvasSpot.getStyle();
* // { width: '100px', height: '...', ... }
*/
getStyle(opts: { boxRect?: BoxRect } & GetBoxRectOptions = {}): Partial<CSSStyleDeclaration> {
const { width, height, x, y } = opts.boxRect || this.getBoxRect(opts);

Expand All @@ -89,6 +134,13 @@ export default class CanvasSpot<T extends CanvasSpotProps = CanvasSpotProps> ext
};
}

/**
* Check the spot type.
* @param {String} type
* @returns {Boolean}
* @example
* canvasSpot.isType('select');
*/
isType<E extends T>(type: E['type']): this is CanvasSpot<E> {
return this.type === type;
}
Expand Down

0 comments on commit b495cfc

Please sign in to comment.