Как правильно называется окно, допустим на формумах, где нужно водить ответ и имеется…
- User
Как правильно называется окно, допустим на формумах, где нужно водить ответ и имеется функции выделения текста(жирный, курсив и так далее)
- Assistant
Такое окно называется редактором текста.
- User
Напиши мне такой редактор с базовыми функциями
- Assistant
- User
Напиши мне это для вставки на сайт используя только html, css, js
- Assistant
Конечно, вот пример простого редактора текста для вставки на сайт, используя только HTML, CSS и JS:
HTML:
<div class="editor"> <div class="toolbar"> <button class="bold">Жирный</button> <button class="italic">Курсив</button> </div> <div contenteditable="true" class="text-editor"></div> <button class="save">Сохранить</button> <input class="file-input" type="file" style="display: none;"> </div> - User
Добавь к этому функции выбора шрифта, размера, заголовков, подчёркивания и добавления фотографий
- Assistant
- Assistant
Чем-то еще могу помочь?
- User
Изменения заголовков не работают, выведи только часть которую нужно исправить
- Assistant
Конечно, извините за ошибку. Вот исправленный JS-код, который должен корректно работать с выбором заголовков:
const textEditor = document.querySelector('.text-editor'); const saveButton = document.querySelector('.save'); const fileInput = document.querySelector('.file-input'); const fontFamily = document.querySelector('.font-family'); const fontSize = document.querySelector('.font-size'); const heading = document.querySelector('.heading'); const boldButton = document.querySelector('.bold'); const italicButton = document.querySelector('.italic'); const underlineButton = document.querySelector('.underline'); const insertImageButton = document.querySelector('.insert-image'); fontFamily.addEventListener('change', () => { document.execCommand('fontName', false, fontFamily.value); }); fontSize.addEventListener('change', () => { document.execCommand('fontSize', false, fontSize.value); }); boldButton.addEventListener('click', () => { document.execCommand('bold', false, null); }); italicButton.addEventListener('click', () => { document.execCommand('italic', false, null); }); underlineButton.addEventListener('click', () => { document.execCommand('underline', false, null); }); heading.addEventListener('change', () => { document.execCommand('formatBlock', false, heading.value); }); insertImageButton.addEventListener('click', () => { fileInput.click(); }); saveButton.addEventListener('click', () => { const text = textEditor.innerHTML; const file = new Blob([text], {type: 'text/plain'}); const fileName = prompt('Введите имя файла', 'file.txt'); if (fileName) { const a = document.createElement('a'); a.href = URL.createObjectURL(file); a.download = fileName; a.click(); } }); fileInput.addEventListener('change', () => { const file = fileInput.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { const img = '<img src="' + reader.result + '">'; document.execCommand('insertHTML', false, img); }; }); - User
Ошибка в коде, изменения заголовков распространяется на весь текст
- Assistant
Извините за неудобства. Вот исправленный JS-код:
const textEditor = document.querySelector('.text-editor'); const saveButton = document.querySelector('.save'); const fileInput = document.querySelector('.file-input'); const fontFamily = document.querySelector('.font-family'); const fontSize = document.querySelector('.font-size'); const heading = document.querySelector('.heading'); const boldButton = document.querySelector('.bold'); const italicButton = document.querySelector('.italic'); const underlineButton = document.querySelector('.underline'); const insertImageButton = document.querySelector('.insert-image'); fontFamily.addEventListener('change', () => { document.execCommand('fontName', false, fontFamily.value); }); fontSize.addEventListener('change', () => { document.execCommand('fontSize', false, fontSize.value); }); boldButton.addEventListener('click', () => { document.execCommand('bold', false, null); }); italicButton.addEventListener('click', () => { document.execCommand('italic', false, null); }); underlineButton.addEventListener('click', () => { document.execCommand('underline', false, null); }); heading.addEventListener('change', () => { const sel = document.getSelection(); if (sel.rangeCount) { const el = document.createElement(heading.value); const range = sel.getRangeAt(0); range.surroundContents(el); } }); insertImageButton.addEventListener('click', () => { fileInput.click(); }); saveButton.addEventListener('click', () => { const text = textEditor.innerHTML; const file = new Blob([text], {type: 'text/plain'}); const fileName = prompt('Введите имя файла', 'file.txt'); if (fileName) { const a = document.createElement('a'); a.href = URL.createObjectURL(file); a.download = fileName; a.click(); } }); fileInput.addEventListener('change', () => { const file = fileInput.files[0]; const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => { const img = '<img src="' + reader.result + '">'; document.execCommand('insertHTML', false, img); }; }); - User
в дальнейшем используй вместо ’ "
- Assistant
Хорошо, в следующий раз я буду использовать