| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Frama-C Utilities |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
* @packageDocumentation |
| 29 |
* @module frama-c/richtext |
| 30 |
*/ |
| 31 |
|
| 32 |
import React from 'react'; |
| 33 |
import * as Dome from 'dome'; |
| 34 |
import * as DomeBuffers from 'dome/text/buffers'; |
| 35 |
import * as KernelData from 'frama-c/kernel/api/data'; |
| 36 |
import { classes } from 'dome/misc/utils'; |
| 37 |
|
| 38 |
const D = new Dome.Debug('Utils'); |
| 39 |
|
| 40 |
// -------------------------------------------------------------------------- |
| 41 |
// --- Print Utilities |
| 42 |
// -------------------------------------------------------------------------- |
| 43 |
|
| 44 |
/** |
| 45 |
* Print text containing tags into buffer. |
| 46 |
* @param buffer Rich text buffer to print into. |
| 47 |
* @param text Actual text containing tags. |
| 48 |
* @param options Specify particular marker options. |
| 49 |
*/ |
| 50 |
export function printTextWithTags( |
| 51 |
buffer: DomeBuffers.RichTextBuffer, |
| 52 |
text: KernelData.text, |
| 53 |
options?: DomeBuffers.MarkerProps, |
| 54 |
): void { |
| 55 |
if (Array.isArray(text)) { |
| 56 |
const tag = text[0]; |
| 57 |
const marker = typeof (tag) === 'string'; |
| 58 |
if (marker) { |
| 59 |
buffer.openTextMarker({ id: tag, ...options ?? {} }); |
| 60 |
} |
| 61 |
for (let k = marker ? 1 : 0; k < text.length; k++) { |
| 62 |
printTextWithTags(buffer, text[k], options); |
| 63 |
} |
| 64 |
if (marker) { |
| 65 |
buffer.closeTextMarker(); |
| 66 |
} |
| 67 |
} else if (typeof text === 'string') { |
| 68 |
buffer.append(text); |
| 69 |
} else { |
| 70 |
D.error('Unexpected text', text); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// -------------------------------------------------------------------------- |
| 75 |
// --- Lightweight Text Renderer |
| 76 |
// -------------------------------------------------------------------------- |
| 77 |
|
| 78 |
interface MarkerProps { |
| 79 |
marker: string; |
| 80 |
onMarker?: (marker: string) => void; |
| 81 |
children?: React.ReactNode; |
| 82 |
} |
| 83 |
|
| 84 |
function Marker(props: MarkerProps): JSX.Element { |
| 85 |
const { marker, onMarker, children } = props; |
| 86 |
const onClick = (): void => { if (onMarker) onMarker(marker); }; |
| 87 |
return ( |
| 88 |
<span |
| 89 |
className="kernel-text-marker" |
| 90 |
onClick={onClick} |
| 91 |
> |
| 92 |
{children} |
| 93 |
</span> |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
function makeContents(text: KernelData.text): React.ReactNode { |
| 98 |
if (Array.isArray(text)) { |
| 99 |
const tag = text[0]; |
| 100 |
const marker = tag && typeof (tag) === 'string'; |
| 101 |
const array = marker ? text.slice(1) : text; |
| 102 |
const contents = React.Children.toArray(array.map(makeContents)); |
| 103 |
if (marker) { |
| 104 |
return <Marker marker={tag}>{contents}</Marker>; |
| 105 |
} |
| 106 |
return <>{contents}</>; |
| 107 |
} if (typeof text === 'string') { |
| 108 |
return text; |
| 109 |
} |
| 110 |
D.error('Unexpected text', text); |
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
export interface TextProps { |
| 115 |
text: KernelData.text; |
| 116 |
onMarker?: (marker: string) => void; |
| 117 |
className?: string; |
| 118 |
} |
| 119 |
|
| 120 |
export function Text(props: TextProps): JSX.Element { |
| 121 |
const className = classes('kernel-text', 'dome-text-code', props.className); |
| 122 |
return <div className={className}>{makeContents(props.text)}</div>; |
| 123 |
} |
| 124 |
|
| 125 |
// -------------------------------------------------------------------------- |