์ž˜๊ฐ€์š” Document.execCommand()๐Ÿšญ

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ์ค‘ ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜์—ฌ ์ดˆ๋Œ€ํ•˜๊ธฐ ๊ธฐ๋Šฅ์ด ์š”๊ตฌ๋˜์–ด ์ƒ์„ฑ๋œ uuid๋‚˜ ๋งํฌ๋ฅผ ํด๋ฆฝ๋ณด๋“œ์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ณผ์ • ์ค‘ ์ฐพ์•„๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

๐Ÿ”ฅDocument.execCommand()

์„ ํƒ ์˜์—ญ์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ์šฐ์„  ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์› ๊ฐ€๋Šฅํ•œ execCommand API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

document๊ฐ€ designmode, ์ฆ‰ ํŽธ์ง‘ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๊ฐ€ ๋˜๋ฉด exeCommand()๋งค์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ๋ฆฐ execCommand๋ฅผ ํ˜ธ์ถœ ํ›„ copy ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

	const onCopy = (e: MouseEvent<HTMLButtonElement>) => {
		e.preventDefault();
		const content = uuid.current;
      	//uuid => ref๋กœ ๋ฐ›์•„์˜จ inputBox์˜ current
		if (content) {
			content.select();          	
			document.execCommand("copy")
		}
	};

๋กœ์ง์€ input๊ฐ™์€ form ์—˜๋ฆฌ๋จผํŠธ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ select()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์„ ํƒ ์˜์—ญ์„ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. (uuid.current.value๋Š” string type์ด๊ธฐ ๋•Œ๋ฌธ์— error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค)

select๋กœ ์„ ํƒํ•œ ์˜์—ญ์„ ๋ฒ”์œ„๋กœ execCommand์˜ copy๋ฉ”์„œ๋“œ๊ฐ€ ์ž‘๋™ํ•˜๋ฉฐ ํด๋ฆฝ๋ณด๋“œ์— ์ •์ƒ์ ์œผ๋กœ ์ €์žฅ์ด ๋ฉ๋‹ˆ๋‹ค.

์ด๊ฑฐ ๋ง๊ณ ๋„ ๋ธŒ๋ผ์šฐ์ €๋‚˜ ๋‹ค๋ฅธ ์š”์ธ์œผ๋กœ ๋‹ค๋ฅด๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์˜ˆ๋กœ ์‚ฌํŒŒ๋ฆฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” focus๋งค์„œ๋“œ๋ฅผ ๊ฑฐ์ณ์•ผํ•จ)

๋ฌธ์ œ๋Š”...


์œ„ ๊ธฐ๋Šฅ์ด ์›น ํ‘œ์ค€์—์„œ ์ œ๊ฑฐ๋˜์–ด Deprecated ๋˜์—ˆ๋‹ค...

โœ…Clipboard API

์•„๋ฌดํŠผ ์•„์ง ํ˜ธํ™˜์ด ์•ˆ๋˜๋Š” ๋ช‡๋ช‡ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์žˆ์ง€๋งŒ ๊ณต์‹๋ฌธ์„œ์—์„œ๋„ execCommand๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ ๋Œ€์ฒดํ•˜์—ฌ Clipboard API ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ ์žˆ๋‹ค.

navigator.clipboard

navigator.clipboard์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ณ„๋„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ฝ๊ณ  ์“ฐ๊ธฐ๋ฅผ ์ž‘๋™์‹œํ‚จ๋‹ค.(read,readText,write,writeText ๋“ฑ๋“ฑ...)

์šฐ๋ฆฌ๋Š” writeText ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ํด๋ฆฝ๋ณด๋“œ์— ์ €์žฅํ• ๊ฒƒ์ด๋‹ค.

์—ฌ๊ธฐ์„œ ํ…์ŠคํŠธ๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค์‹œํ”ผ

์ด์ „์˜ document.execCommand๋Š” form element์˜ ์„ ํƒ์˜์—ญ์„ ๋ณต์‚ฌํ•˜์—ฌ ํด๋ฆฝ๋ณด๋“œ์— ์ €์žฅํ•˜๋Š”, ๋งˆ์น˜ ํ‚ค๋ณด๋“œ๋กœ ๋ณต์‚ฌ, ๋ถ™์—ฌ๋„ฃ๊ธฐ ์ž‘์—…๊ณผ ์œ ์‚ฌํ•˜๋‹ค๋ฉด
navigator.clipboard.writeText๋Š” ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ์ฝ๊ณ , ์“ฐ๋Š” ์ž‘์—…์œผ๋กœ ๋ณด๋ฉด ๋  ๊ฑฐ ๊ฐ™๋‹ค.

	const onCopy = (e: MouseEvent<HTMLButtonElement>) => {
		e.preventDefault();
		const content = uuid.current;
		if (content) {
			navigator.clipboard.writeText(content.value).then(() => {
				alert('์ฝ”๋“œ ๋ณต์‚ฌ');
			});
		}
	};

์ž˜๊ฐ€์š” Document.execCommand()...

Document.execCommand()

navigator.clipboard

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ