File size: 10,864 Bytes
a417977 |
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import style from "./style.module.scss";
import {Preview} from "@/components/preview";
import cn from "classnames";
import {useState, useRef, MutableRef, useEffect} from "preact/hooks";
import {JSX} from "preact";
import {
User,
Type,
Bold,
Italic,
Underline,
ChevronRight,
EyeOff,
Smile,
Image,
} from "preact-feather";
import {smileysMap} from "@/utils/smileys";
import {Button} from "@/components/button";
import {Input} from "@/components/input";
import {Alert} from "@/components/alert";
const iconSize = 14;
// const initialText = "> zefgze https://image.noelshack.com/fichiers/2018/13/4/1522325846-jesusopti.png\n" +
// "> zgfzreg :-)))\n" +
// "> > ergerg\n" +
// "zezefzef\n" +
// "<spoil>loool</spoil> AHI";
export function Wysiwyg(props: {
showTitle: boolean,
onSubmit: (user: string, title: string, text: string) => void,
disabled?: boolean,
initialText?: string,
text?: string,
setText?: (text: string) => void,
}) {
const [errors, setErrors] = useState<string[]>([]);
const [title, setTitle] = useState("");
const [user, setUser] = useState("");
const [text, setText] = props.text !== undefined && props.setText !== undefined ? [props.text, props.setText] : useState(props.initialText || "");
const [accordion, setAccordion] = useState<"smileys"|"stickers"|false>(false);
// Create a ref for the textarea
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const selectionRange = useRef<[start: number, end: number]|null>(null);
// // Example function to demonstrate interaction with the ref
// const focusTextarea = () => {
// if (textareaRef.current) {
// textareaRef.current.focus();
// }
// };
useEffect(() => {
if (textareaRef.current && selectionRange.current) {
const [start, end] = selectionRange.current;
textareaRef.current.setSelectionRange(start, end);
selectionRange.current = null;
}
}, [text])
const bold = createAction(textareaRef, (start, end) => {
const tag = "'''";
setText(insertAt(insertAt(text, tag, start), tag, end + tag.length));
selectionRange.current = [start + tag.length, end + tag.length];
});
const italic = createAction(textareaRef, (start, end) => {
const tag = "''";
setText(insertAt(insertAt(text, tag, start), tag, end + tag.length));
selectionRange.current = [start + tag.length, end + tag.length];
});
const underline = createAction(textareaRef, (start, end) => {
const startTag = "<u>";
const endTag = "</u>";
setText(insertAt(insertAt(text, startTag, start), endTag, end + startTag.length));
selectionRange.current = [start + startTag.length, end + startTag.length];
});
const quote = createAction(textareaRef, (start, end) => {
const lines = text.split("\n");
const newLines: string[] = [];
let charCount = 0;
let quoteEnd = 0;
let addedChars = 0;
for (const line of lines) {
charCount += line.length + 1; // Add "\n"
if (charCount > start && (charCount - line.length) <= end + 1) {
newLines.push(`> ${line}`);
addedChars += 2;
quoteEnd = charCount;
} else {
newLines.push(line);
}
}
setText(newLines.join("\n"));
selectionRange.current = [quoteEnd + addedChars - 1, quoteEnd + addedChars - 1];
});
const spoil = createAction(textareaRef, (start, end) => {
const startTag = "<spoil>";
const endTag = "</spoil>";
setText(insertAt(insertAt(text, startTag, start), endTag, end + startTag.length));
selectionRange.current = [start + startTag.length, end + startTag.length];
});
return (
<div>
<Alert lines={errors}/>
<Input
type="text"
icon={User}
value={user}
onChange={v => setUser(v as string)}
placeholder="Pseudo"
/>
{
props.showTitle ? (
<Input
type="text"
icon={Type}
value={title}
onChange={v => setTitle(v as string)}
placeholder="Titre du sujet"
/>
) : null
}
<div className={style.container}>
<div className={style.toolbar}>
<div className={style.toolbarGroup}>
<div className={style.buttonContainer} title="Gras" onMouseDown={bold}>
<Bold size={iconSize}/>
</div>
<div className={style.buttonContainer} title="Italique" onMouseDown={italic}>
<Italic size={iconSize}/>
</div>
<div className={style.buttonContainer} title="Souligné" onMouseDown={underline}>
<Underline size={iconSize}/>
</div>
</div>
<div className={style.toolbarGroup}>
<div className={style.buttonContainer} title="Citation" onMouseDown={quote}>
<ChevronRight size={iconSize}/>
</div>
<div className={style.buttonContainer} title="Spoil" onMouseDown={spoil}>
<EyeOff size={iconSize}/>
</div>
</div>
<div className={style.toolbarGroup}>
<div
className={style.buttonContainer}
title="Smileys"
onClick={() => {
setAccordion(accordion ? false : "smileys")
}}
>
<Smile size={iconSize}/>
</div>
<div className={style.buttonContainer} title="Stickers">
<Image size={iconSize}/>
</div>
</div>
</div>
<div className={cn(style.accordion, {[style.accordionOpened]: accordion})}>
<SmileyList
text={text}
setText={setText}
textareaRef={textareaRef}
selectionRange={selectionRange}
/>
</div>
<div className={style.editor}>
<textarea
className={style.textarea}
placeholder="Saisissez un message…"
name="wysiwyg"
id="wysiwyg"
cols="30"
rows="10"
value={text}
onInput={(e) => setText((e.target as HTMLTextAreaElement).value)}
ref={textareaRef} // Attach ref to textarea
/>
</div>
</div>
<div className={cn(style.container, style.previewContainer)}>
<Preview raw={text}/>
</div>
<Button
secondary={true}
onClick={() => {
const errors: string[] = [];
if(user.length < 2) {
errors.push("Le pseudo doit se composer au minimum de 3 caractères");
}
if(user.match(/[^a-zA-Z0-9_-]/)) {
errors.push("Le pseudo doit uniquement contenir les caractères suivant: a-z, A-Z, 0-9, _ et -");
}
if(props.showTitle && title.length < 2) {
errors.push("Le titre du sujet doit se composer au minimum de 3 caractères");
}
if(text.length < 2) {
errors.push("Le text du sujet doit se composer au minimum de 3 caractères");
}
setErrors(errors);
if(errors.length > 0) {
return;
}
props.onSubmit(user, title, text);
setUser("");
setTitle("");
setText("");
}}
disabled={props.disabled}
>
Poster
</Button>
</div>
);
}
function SmileyList(props: {
text: string,
setText: (text: string) => void,
textareaRef: MutableRef<HTMLTextAreaElement | null>,
selectionRange: MutableRef<[start: number, end: number]|null>,
}) {
// const addSmiley = createAction(props.textareaRef, (start, end) => {
// const startTag = "<u>";
// const endTag = "</u>";
//
// props.setText(insertAt(insertAt(props.text, startTag, start), endTag, end + startTag.length));
// props.selectionRange.current = [start + startTag.length, end + startTag.length];
// });
const addSmiley: (smiley: string) => JSX.MouseEventHandler<HTMLDivElement> = (smiley) => (e) => {
e.preventDefault(); // Do not lose the focus on textarea
if (props.textareaRef.current) {
props.setText(insertAt(props.text, smiley, props.textareaRef.current.selectionStart));
props.selectionRange.current = [props.textareaRef.current.selectionStart + smiley.length, props.textareaRef.current.selectionStart + smiley.length];
props.textareaRef.current.focus();
}
}
return (
<div className={style.smileyList}>
{smileysMap.map((smiley) => (
<div onMouseDown={addSmiley(` ${smiley[0]}`)}>
<img
src={`https://image.jeuxvideo.com/smileys_img/${smiley[1]}.gif`}
alt={smiley[0]}
/>
</div>
))}
</div>
)
}
function createAction(
ref: MutableRef<HTMLTextAreaElement | null>,
callback: (start: number, end: number) => void
): JSX.MouseEventHandler<HTMLDivElement> {
return (e) => {
e.preventDefault(); // Do not lose the focus on textarea
if (ref.current) {
callback(ref.current.selectionStart, ref.current.selectionEnd);
ref.current.focus();
}
}
}
function insertAt(originalString: string, substring: string, position: number): string {
return originalString.slice(0, position) + substring + originalString.slice(position);
} |