File size: 5,671 Bytes
8d9d326 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { MarkdownCode } from "@gradio/markdown-code";
import type { I18nFormatter } from "@gradio/utils";
import type { CellValue } from "./types";
import SelectionButtons from "./icons/SelectionButtons.svelte";
import BooleanCell from "./BooleanCell.svelte";
export let edit: boolean;
export let value: CellValue = "";
export let display_value: string | null = null;
export let styling = "";
export let header = false;
export let datatype:
| "str"
| "markdown"
| "html"
| "number"
| "bool"
| "date"
| "image" = "str";
export let latex_delimiters: {
left: string;
right: string;
display: boolean;
}[];
export let line_breaks = true;
export let editable = true;
export let is_static = false;
export let max_chars: number | null = null;
export let components: Record<string, any> = {};
export let i18n: I18nFormatter;
export let is_dragging = false;
export let wrap_text = false;
export let show_selection_buttons = false;
export let coords: [number, number];
export let on_select_column: ((col: number) => void) | null = null;
export let on_select_row: ((row: number) => void) | null = null;
export let el: HTMLTextAreaElement | null;
const dispatch = createEventDispatcher<{
blur: { blur_event: FocusEvent; coords: [number, number] };
keydown: KeyboardEvent;
}>();
function truncate_text(
text: CellValue,
max_length: number | null = null,
is_image = false
): string {
if (is_image) return String(text);
const str = String(text);
if (!max_length || max_length <= 0) return str;
if (str.length <= max_length) return str;
return str.slice(0, max_length) + "...";
}
$: should_truncate = !edit && max_chars !== null && max_chars > 0;
$: display_content = editable
? value
: display_value !== null
? display_value
: value;
$: display_text = should_truncate
? truncate_text(display_content, max_chars, datatype === "image")
: display_content;
function use_focus(node: HTMLTextAreaElement): any {
requestAnimationFrame(() => {
node.focus();
});
return {};
}
function handle_blur(event: FocusEvent): void {
dispatch("blur", {
blur_event: event,
coords: coords
});
}
function handle_keydown(event: KeyboardEvent): void {
dispatch("keydown", event);
}
function commit_change(checked: boolean): void {
handle_blur({ target: { value } } as unknown as FocusEvent);
}
$: if (!edit) {
// Shim blur on removal for Safari and Firefox
handle_blur({ target: { value } } as unknown as FocusEvent);
}
</script>
{#if edit && datatype !== "bool"}
<textarea
readonly={is_static}
aria-readonly={is_static}
aria-label={is_static ? "Cell is read-only" : "Edit cell"}
bind:this={el}
bind:value
class:header
tabindex="-1"
on:blur={handle_blur}
on:mousedown|stopPropagation
on:click|stopPropagation
use:use_focus
on:keydown={handle_keydown}
/>
{/if}
{#if datatype === "bool" && typeof value === "boolean"}
<BooleanCell bind:value {editable} on_change={commit_change} />
{:else}
<span
class:dragging={is_dragging}
on:keydown={handle_keydown}
tabindex="0"
role="button"
class:edit
class:expanded={edit}
class:multiline={header}
on:focus|preventDefault
style={styling}
data-editable={editable}
data-max-chars={max_chars}
data-expanded={edit}
placeholder=" "
class:text={datatype === "str"}
class:wrap={wrap_text}
>
{#if datatype === "image" && components.image}
<svelte:component
this={components.image}
value={{ url: display_text }}
show_label={false}
label="cell-image"
show_download_button={false}
{i18n}
gradio={{ dispatch: () => {} }}
/>
{:else if datatype === "html"}
{@html display_text}
{:else if datatype === "markdown"}
<MarkdownCode
message={display_text.toLocaleString()}
{latex_delimiters}
{line_breaks}
chatbot={false}
/>
{:else}
{display_text}
{/if}
</span>
{/if}
{#if show_selection_buttons && coords && on_select_column && on_select_row}
<SelectionButtons
position="column"
{coords}
on_click={() => on_select_column(coords[1])}
/>
<SelectionButtons
position="row"
{coords}
on_click={() => on_select_row(coords[0])}
/>
{/if}
<style>
.dragging {
cursor: crosshair !important;
}
textarea {
position: absolute;
flex: 1 1 0%;
transform: translateX(-0.1px);
outline: none;
border: none;
background: transparent;
cursor: text;
width: calc(100% - var(--size-2));
resize: none;
height: 100%;
padding-left: 0;
font-size: inherit;
font-weight: inherit;
line-height: var(--line-lg);
}
textarea:focus {
outline: none;
}
span {
flex: 1 1 0%;
position: relative;
display: inline-block;
outline: none;
-webkit-user-select: text;
-moz-user-select: text;
-ms-user-select: text;
user-select: text;
cursor: text;
width: 100%;
height: 100%;
overflow: hidden;
}
span.text.expanded {
height: auto;
min-height: 100%;
white-space: pre-wrap;
word-break: break-word;
overflow: visible;
}
.multiline {
white-space: pre;
overflow: hidden;
text-overflow: ellipsis;
}
.header {
transform: translateX(0);
font-weight: var(--weight-bold);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-left: var(--size-1);
}
.edit {
opacity: 0;
pointer-events: none;
}
span :global(img) {
max-height: 100px;
width: auto;
object-fit: contain;
}
textarea:read-only {
cursor: not-allowed;
}
.wrap,
.wrap.expanded {
white-space: normal;
word-wrap: break-word;
overflow-wrap: break-word;
word-wrap: break-word;
}
</style>
|