mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-31 14:16:42 +02:00
refactor: Address review comments
Moved some types around Removed unnecessary params Co-authored-by: Reda Al Sulais <u.yokozuna@gmail.com> Co-authored-by: Alois Klink <alois@aloisklink.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { imgSnapshotTest, renderGraph } from '../../helpers/util';
|
import { imgSnapshotTest } from '../../helpers/util';
|
||||||
|
|
||||||
describe('packet structure', () => {
|
describe('packet structure', () => {
|
||||||
it('should render a simple packet diagram', () => {
|
it('should render a simple packet diagram', () => {
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[defaultConfig.ts:272](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L272)
|
[defaultConfig.ts:275](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L275)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@@ -259,10 +259,6 @@ const config: RequiredDeep<MermaidConfig> = {
|
|||||||
},
|
},
|
||||||
packet: {
|
packet: {
|
||||||
...defaultConfigJson.packet,
|
...defaultConfigJson.packet,
|
||||||
useWidth: undefined,
|
|
||||||
// this is false, unlike every other diagram (other than gitGraph)
|
|
||||||
// TODO: can we make this default to `true` instead?
|
|
||||||
useMaxWidth: false,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -1,13 +1,9 @@
|
|||||||
import type { Block, PacketDB, Row } from './types.js';
|
import type { Block, PacketDB, PacketData, Row } from './types.js';
|
||||||
import type { PacketDiagramConfig } from '../../config.type.js';
|
import type { PacketDiagramConfig } from '../../config.type.js';
|
||||||
|
|
||||||
import { log } from '../../logger.js';
|
import { log } from '../../logger.js';
|
||||||
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
import DEFAULT_CONFIG from '../../defaultConfig.js';
|
||||||
import { getConfig as commonGetConfig } from '../../config.js';
|
import { getConfig as commonGetConfig } from '../../config.js';
|
||||||
|
import { cleanAndMerge } from '../../utils.js';
|
||||||
interface PacketData {
|
|
||||||
packet: Row[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultPacketData: PacketData = {
|
const defaultPacketData: PacketData = {
|
||||||
packet: [],
|
packet: [],
|
||||||
@@ -17,7 +13,7 @@ let data: PacketData = structuredClone(defaultPacketData);
|
|||||||
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
|
export const DEFAULT_PACKET_CONFIG: Required<PacketDiagramConfig> = DEFAULT_CONFIG.packet;
|
||||||
|
|
||||||
export const getConfig = (): Required<PacketDiagramConfig> => {
|
export const getConfig = (): Required<PacketDiagramConfig> => {
|
||||||
const config = structuredClone({
|
const config = cleanAndMerge({
|
||||||
...DEFAULT_PACKET_CONFIG,
|
...DEFAULT_PACKET_CONFIG,
|
||||||
...commonGetConfig().packet,
|
...commonGetConfig().packet,
|
||||||
});
|
});
|
||||||
@@ -61,9 +57,9 @@ export const getNextFittingBlock = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const populate = ({ blocks }: { blocks: Block[] }) => {
|
export const populate = ({ blocks }: { blocks: Block[] }) => {
|
||||||
|
clear();
|
||||||
let lastByte = -1;
|
let lastByte = -1;
|
||||||
let word: Row = [];
|
let word: Row = [];
|
||||||
data.packet = [];
|
|
||||||
let row = 1;
|
let row = 1;
|
||||||
const { bitsPerRow } = getConfig();
|
const { bitsPerRow } = getConfig();
|
||||||
for (let { start, end, label } of blocks) {
|
for (let { start, end, label } of blocks) {
|
||||||
@@ -97,7 +93,6 @@ export const populate = ({ blocks }: { blocks: Block[] }) => {
|
|||||||
if (word.length > 0) {
|
if (word.length > 0) {
|
||||||
data.packet.push(word);
|
data.packet.push(word);
|
||||||
}
|
}
|
||||||
log.debug(data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clear = () => {
|
export const clear = () => {
|
||||||
@@ -107,4 +102,5 @@ export const clear = () => {
|
|||||||
export const db: PacketDB = {
|
export const db: PacketDB = {
|
||||||
getPacket,
|
getPacket,
|
||||||
getConfig,
|
getConfig,
|
||||||
|
clear,
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
|
import type { DiagramStylesProvider } from '../../diagram-api/types.js';
|
||||||
import { log } from '../../logger.js';
|
import { log } from '../../logger.js';
|
||||||
|
import type { PacketStyleOptions } from './types.js';
|
||||||
|
|
||||||
export const styles = (options: any = {}) => {
|
export const styles: DiagramStylesProvider = (options: { packet?: PacketStyleOptions } = {}) => {
|
||||||
log.debug({ options });
|
log.debug({ options });
|
||||||
return `
|
return `
|
||||||
.byte {
|
.byte {
|
||||||
|
@@ -9,4 +9,20 @@ export type Row = Required<Block>[];
|
|||||||
export interface PacketDB extends DiagramDB {
|
export interface PacketDB extends DiagramDB {
|
||||||
getPacket: () => Row[];
|
getPacket: () => Row[];
|
||||||
getConfig: () => Required<PacketDiagramConfig>;
|
getConfig: () => Required<PacketDiagramConfig>;
|
||||||
|
clear: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PacketStyleOptions {
|
||||||
|
byteFontSize?: string;
|
||||||
|
startByteColor?: string;
|
||||||
|
endByteColor?: string;
|
||||||
|
labelColor?: string;
|
||||||
|
labelFontSize?: string;
|
||||||
|
blockStrokeColor?: string;
|
||||||
|
blockStrokeWidth?: string;
|
||||||
|
blockFillColor?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PacketData {
|
||||||
|
packet: Row[];
|
||||||
}
|
}
|
||||||
|
@@ -2,13 +2,19 @@ grammar Packet
|
|||||||
import "../common/common";
|
import "../common/common";
|
||||||
|
|
||||||
entry Packet:
|
entry Packet:
|
||||||
"packet-beta" NEWLINE*
|
NEWLINE*
|
||||||
TitleAndAccessibilities?
|
"packet-beta"
|
||||||
(blocks+=Block)*;
|
(
|
||||||
|
NEWLINE* TitleAndAccessibilities blocks+=PacketBlock*
|
||||||
|
| NEWLINE+ blocks+=PacketBlock+
|
||||||
|
| NEWLINE*
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
Block:
|
PacketBlock:
|
||||||
start=INT ('-' end=INT)? ':' label=STRING;
|
start=INT('-' end=INT)? ':' label=STRING NEWLINE+
|
||||||
|
;
|
||||||
|
|
||||||
hidden terminal WS: /\s+/;
|
hidden terminal WS: /\s+/;
|
||||||
terminal INT returns number: /[0-9]+/;
|
terminal INT returns number: /0|[1-9][0-9]*/;
|
||||||
terminal STRING: /"[^"]*"|'[^']*'/;
|
terminal STRING: /"[^"]*"|'[^']*'/;
|
||||||
|
Reference in New Issue
Block a user