youssefreda9 commited on
Commit
df46576
·
1 Parent(s): 92557ee

fix: Strip HTML formatting on paste into editor - Added paste event handler that converts clipboard to plain text - Prevents rich text styling (colors, opacity, fonts) from being carried over - Preserves paragraph structure with line breaks - Fixes ghost formatting when pasting from chat or web pages

Browse files
Files changed (1) hide show
  1. src/js/editor.js +38 -0
src/js/editor.js CHANGED
@@ -86,6 +86,44 @@ function initEditor() {
86
  } catch (e) {}
87
  });
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  editor.addEventListener('click', (e) => {
90
  handleEditorClick(e);
91
  });
 
86
  } catch (e) {}
87
  });
88
 
89
+ // Strip formatting on paste — prevent rich HTML (colors, opacity, fonts)
90
+ // from being carried over when pasting from chat, web pages, etc.
91
+ editor.addEventListener('paste', (e) => {
92
+ e.preventDefault();
93
+ const text = (e.clipboardData || window.clipboardData).getData('text/plain');
94
+ if (!text) return;
95
+
96
+ // Insert plain text at cursor position
97
+ const selection = window.getSelection();
98
+ if (!selection.rangeCount) return;
99
+
100
+ const range = selection.getRangeAt(0);
101
+ range.deleteContents();
102
+
103
+ // Split by newlines to preserve paragraph structure
104
+ const lines = text.split(/\r?\n/);
105
+ const fragment = document.createDocumentFragment();
106
+ lines.forEach((line, i) => {
107
+ if (i > 0) fragment.appendChild(document.createElement('br'));
108
+ fragment.appendChild(document.createTextNode(line));
109
+ });
110
+
111
+ range.insertNode(fragment);
112
+
113
+ // Move cursor to end of pasted content
114
+ range.collapse(false);
115
+ selection.removeAllRanges();
116
+ selection.addRange(range);
117
+
118
+ // Trigger editor update
119
+ updateEditorStats();
120
+ updatePlaceholder();
121
+ analyzeTextDelayed();
122
+ try {
123
+ localStorage.setItem('bayan_editor_draft', editor.innerHTML);
124
+ } catch (e) {}
125
+ });
126
+
127
  editor.addEventListener('click', (e) => {
128
  handleEditorClick(e);
129
  });