updated image shape

This commit is contained in:
omkarht
2024-09-10 21:22:28 +05:30
parent 194684e27b
commit 1cc2f323de
2 changed files with 33 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
import { imgSnapshotTest } from '../../helpers/util'; import { imgSnapshotTest } from '../../helpers/util';
const looks = ['classic', 'handDrawn'] as const; const looks = ['classic', 'handDrawn'] as const;
const directions = ['TB', 'BT', 'LR', 'RL'] as const; const directions = ['TB', 'LR'] as const;
const labelPos = [undefined, 't', 'b'] as const; const labelPos = [undefined, 't', 'b'] as const;
looks.forEach((look) => { looks.forEach((look) => {
@@ -18,7 +18,7 @@ looks.forEach((look) => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is a label for image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is a label for image shape'`;
flowchartCode += `, w: '200', h: '200'`; flowchartCode += `, w: '100', h: '200'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
} }
@@ -30,7 +30,7 @@ looks.forEach((look) => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is a very very very very very long long long label for image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is a very very very very very long long long label for image shape'`;
flowchartCode += `, w: '100', h: '100'`; flowchartCode += `, w: '100', h: '250'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
} }
@@ -42,17 +42,18 @@ looks.forEach((look) => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is **bold** </br>and <strong>strong</strong> for image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is **bold** </br>and <strong>strong</strong> for image shape'`;
flowchartCode += `, w: '450', h: '550'`; flowchartCode += `, w: '550', h: '200'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
} }
flowchartCode += ` }@\n`; flowchartCode += ` }@\n`;
imgSnapshotTest(flowchartCode, { look }); imgSnapshotTest(flowchartCode, { look, htmlLabels: true });
}); });
it(`with markdown htmlLabels:false`, () => { it(`with markdown htmlLabels:false`, () => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is **bold** </br>and <strong>strong</strong> for image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'This is **bold** </br>and <strong>strong</strong> for image shape'`;
flowchartCode += `, w: '250', h: '200'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
@@ -68,6 +69,7 @@ looks.forEach((look) => {
it(`with styles`, () => { it(`with styles`, () => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'new image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'new image shape'`;
flowchartCode += `, w: '550', h: '200'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
@@ -79,10 +81,10 @@ looks.forEach((look) => {
it(`with classDef`, () => { it(`with classDef`, () => {
let flowchartCode = `flowchart ${direction}\n`; let flowchartCode = `flowchart ${direction}\n`;
flowchartCode += ` classDef customClazz fill:#bbf,stroke:#f66,stroke-width:2px,color:#fff,stroke-dasharray: 5 5\n`; flowchartCode += ` classDef customClazz fill:#bbf,stroke:#f66,stroke-width:2px,color:#000000,stroke-dasharray: 5 5\n`;
flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'new image shape'`; flowchartCode += ` nA --> A@{ img: 'https://cdn.pixabay.com/photo/2020/02/22/18/49/paper-4871356_1280.jpg', label: 'new image shape'`;
flowchartCode += `, w: '450', h: '550'`; flowchartCode += `, w: '500', h: '550'`;
if (pos) { if (pos) {
flowchartCode += `, pos: '${pos}'`; flowchartCode += `, pos: '${pos}'`;
} }

View File

@@ -6,12 +6,11 @@ import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs'; import rough from 'roughjs';
import intersect from '../intersect/index.js'; import intersect from '../intersect/index.js';
import { createPathFromPoints } from './util.js'; import { createPathFromPoints } from './util.js';
import { getConfig } from '../../../diagram-api/diagramAPI.js';
export const imageSquare = async (parent: SVG, node: Node) => { export const imageSquare = async (parent: SVG, node: Node) => {
const { labelStyles, nodeStyles } = styles2String(node); const { labelStyles, nodeStyles } = styles2String(node);
node.labelStyle = labelStyles; node.labelStyle = labelStyles;
const { shapeSvg, bbox, label } = await labelHelper(parent, node, getNodeClasses(node));
const { cssStyles } = node;
const img = new Image(); const img = new Image();
img.src = node?.img ?? ''; img.src = node?.img ?? '';
@@ -22,15 +21,22 @@ export const imageSquare = async (parent: SVG, node: Node) => {
const imageWidth = node?.assetWidth ?? imageNaturalWidth; const imageWidth = node?.assetWidth ?? imageNaturalWidth;
const imageHeight = node?.assetHeight ?? imageNaturalHeight; const imageHeight = node?.assetHeight ?? imageNaturalHeight;
const defaultWidth = getConfig().flowchart?.wrappingWidth;
// const htmlLabels = getConfig().flowchart?.htmlLabels
node.width = Math.max(node.width ?? 0, node.label ? (defaultWidth ?? 0) : 0, imageWidth);
const { shapeSvg, bbox, label } = await labelHelper(parent, node, getNodeClasses(node));
const { cssStyles } = node;
const defaultHeight = bbox.height;
node.height = Math.max(node.height ?? 0, node.label ? (defaultHeight ?? 0) : 0, imageHeight);
const width = Math.max(bbox.width + (node.padding ?? 0), imageWidth, node?.width ?? 0); const width = Math.max(bbox.width + (node.padding ?? 0), imageWidth, node?.width ?? 0);
const height = Math.max(bbox.height + (node.padding ?? 0), imageHeight, node?.height ?? 0); const height = Math.max(bbox.height + (node.padding ?? 0), imageHeight, node?.height ?? 0);
// const imageSizeWidth = width / 2; const skeletonWidth = width + (node?.padding ?? 0) / 2;
const imageSizeHeight = height * 0.1; const skeletonHeight = height + bbox.height;
const skeletonWidth = width + (node?.padding ?? 0);
const skeletonHeight = height + imageSizeHeight + bbox.height;
const imagePosition = node?.pos ?? 'b'; const imagePosition = node?.pos ?? 'b';
const points = [ const points = [
@@ -52,31 +58,32 @@ export const imageSquare = async (parent: SVG, node: Node) => {
const linePath = createPathFromPoints(points); const linePath = createPathFromPoints(points);
const lineNode = rc.path(linePath, options); const lineNode = rc.path(linePath, options);
const iconShape = shapeSvg.insert(() => lineNode, ':first-child'); const imageShape = shapeSvg.insert(() => lineNode, ':first-child').attr('opacity', 0);
if (node.img) { if (node.img) {
const image = shapeSvg.append('image'); const image = shapeSvg.append('image');
// Set the image attributes // Set the image attributes
image.attr('href', node.img); image.attr('href', node.img);
image.attr('width', imageWidth); image.attr('width', node.width);
image.attr('height', imageHeight); image.attr('height', node.height);
image.attr('preserveAspectRatio', 'none');
const yPos = const yPos =
imagePosition === 'b' imagePosition === 'b'
? -(skeletonHeight / 2 - imageSizeHeight / 2) ? -(skeletonHeight / 2)
: skeletonHeight / 2 - height - imageSizeHeight / 2; : skeletonHeight / 2 - height + (bbox.y + (bbox.top ?? 0)) * 2;
image.attr('transform', `translate(${-imageWidth / 2}, ${yPos})`); image.attr('transform', `translate(${-node.width / 2}, ${yPos})`);
} }
if (cssStyles && node.look !== 'handDrawn') { if (cssStyles && node.look !== 'handDrawn') {
iconShape.selectAll('path').attr('style', cssStyles); imageShape.selectAll('path').attr('style', cssStyles);
} }
if (nodeStyles && node.look !== 'handDrawn') { if (nodeStyles && node.look !== 'handDrawn') {
iconShape.selectAll('path').attr('style', nodeStyles); imageShape.selectAll('path').attr('style', nodeStyles);
} }
iconShape.attr('transform', `translate(${-skeletonWidth / 2},${-skeletonHeight / 2})`); imageShape.attr('transform', `translate(${-skeletonWidth / 2},${-skeletonHeight / 2})`);
const yPos = const yPos =
imagePosition === 'b' imagePosition === 'b'
@@ -85,7 +92,7 @@ export const imageSquare = async (parent: SVG, node: Node) => {
label.attr('transform', `translate(${-bbox.width / 2 - (bbox.x - (bbox.left ?? 0))},${yPos})`); label.attr('transform', `translate(${-bbox.width / 2 - (bbox.x - (bbox.left ?? 0))},${yPos})`);
updateNodeBounds(node, iconShape); updateNodeBounds(node, imageShape);
node.intersect = function (point) { node.intersect = function (point) {
log.info('iconSquare intersect', node, point); log.info('iconSquare intersect', node, point);