@@ -67,7 +67,7 @@ import temp57 from "../assets/images/memeTemplate/temp57.png";
6767export default function Create ( ) {
6868 const imageContainer = useRef ( ) ;
6969 const [ memeTemplateView , setMemeTemplate ] = useState ( "" ) ;
70- const [ selectedText , setSelectedText ] = useState ( "" ) ;
70+ const [ selectedText , setSelectedText ] = useState ( "" ) ; // Id of generated element
7171 const [ currentText , setCurrentText ] = useState ( "" ) ;
7272
7373 // useEffect(() => {
@@ -99,6 +99,12 @@ export default function Create() {
9999 setMemeTemplate ( e . target . src ) ;
100100 } ;
101101
102+ const deleteSelected = e => {
103+ if ( selectedText ) {
104+ document . querySelector ( `#${ selectedText } ` ) . remove ( ) ;
105+ }
106+ }
107+
102108 const AddImageToCanvas = ( e ) => {
103109 const newImage = document . createElement ( "img" ) ;
104110 newImage . src = templateFive ;
@@ -159,6 +165,9 @@ export default function Create() {
159165 const newText = document . createElement ( "div" ) ;
160166 const random_id = "meme-" + uuid ( ) ;
161167 newText . setAttribute ( "id" , random_id ) ;
168+ newText . toggleAttribute ( "data-text-underlined" ) ;
169+ newText . toggleAttribute ( "data-text-bold" ) ;
170+ newText . toggleAttribute ( "data-text-italics" ) ;
162171 newText . classList . add ( "meme_text" ) ;
163172 newText . innerText = "Enter text here..." ;
164173 newText . contentEditable = true ;
@@ -211,24 +220,67 @@ export default function Create() {
211220 // Text functions
212221 const textFunctions = {
213222 toggleBold : function ( ) {
223+ // document.querySelector(`#${selectedText}`).classList.toggle("bold");
224+ // document.querySelector(`#${selectedText}`).toggleAttribute("data-text-bold");
225+ // document.querySelector(`#${selectedText}`).style.fontWeight = "bolder";
226+
214227 if ( ! selectedText ) return ;
215- document . querySelector ( `#${ selectedText } ` ) . classList . toggle ( "bold" ) ;
228+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
229+ if ( ! textElem ) return setSelectedText ( "" ) ;
230+ textElem . toggleAttribute ( "data-text-bold" ) ;
231+ if ( textElem . hasAttribute ( "data-text-bold" ) ) {
232+ textElem . style . fontWeight = "bolder" ;
233+ return ;
234+ }
235+ textElem . style . fontWeight = "normal" ;
216236 } ,
217237 toggleItalics : function ( ) {
218238 if ( ! selectedText ) return ;
219- // document.querySelector(`#${selectedText}`).classList.toggle("italics");
220- document . querySelector ( `#${ selectedText } ` ) ;
221-
222- document . execCommand ( "underline" ) ;
239+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
240+ if ( ! textElem ) return setSelectedText ( "" ) ;
241+ textElem . toggleAttribute ( "data-text-italic" ) ;
242+ if ( textElem . hasAttribute ( "data-text-italic" ) ) {
243+ textElem . style . fontStyle = "italic" ;
244+ return ;
245+ }
246+ textElem . style . fontStyle = "normal" ;
223247 } ,
224248 toggleUnderline : function ( ) {
225249 if ( ! selectedText ) return ;
226- document . querySelector ( `#${ selectedText } ` ) . classList . toggle ( "underline" ) ;
250+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
251+ if ( ! textElem ) return setSelectedText ( "" ) ;
252+ textElem . toggleAttribute ( "data-text-underlined" ) ;
253+ if ( textElem . hasAttribute ( "data-text-underlined" ) ) {
254+ textElem . style . textDecoration = "underline" ;
255+ return ;
256+ }
257+ textElem . style . textDecoration = "none" ;
227258 } ,
228259 changeText : function ( e ) {
229260 setCurrentText ( e . target . value ) ;
230- document . getElementById ( selectedText ) . innerText = e . target . value ;
261+ const textElem = document . getElementById ( selectedText ) ;
262+ if ( ! textElem ) return setSelectedText ( "" ) ;
263+ textElem . innerText = e . target . value ;
231264 } ,
265+ changeTextSize : function ( e ) {
266+ if ( ! selectedText ) return ;
267+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
268+ if ( ! textElem ) return setSelectedText ( "" ) ;
269+ textElem . style . fontSize = `${ e . target . value } px` ;
270+ } ,
271+ changeTextColor : function ( e ) {
272+ if ( ! selectedText ) return ;
273+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
274+ if ( ! textElem ) return setSelectedText ( "" ) ;
275+ textElem . style . color = e . target . value ;
276+ } ,
277+ justify : function ( e ) {
278+ console . log ( e . target . dataset [ "justification" ] ) ;
279+ if ( ! selectedText ) return ;
280+ const textElem = document . querySelector ( `#${ selectedText } ` ) ;
281+ if ( ! textElem ) return setSelectedText ( "" ) ;
282+ textElem . style . textAlign = e . target . dataset [ "justification" ] ;
283+ }
232284 } ;
233285
234286 return (
@@ -769,26 +821,26 @@ export default function Create() {
769821
770822 < div >
771823 < p > Font size:</ p >
772- < input type = "text" defaultValue = { 16 } maxLength = { 3 } />
824+ < input type = "text" defaultValue = { 16 } maxLength = { 3 } onChange = { textFunctions . changeTextSize } />
773825 </ div >
774826
775827 < div >
776828 < p > Font color:</ p >
777- < input type = "color" defaultValue = "#000000" > </ input >
829+ < input type = "color" defaultValue = "#ffffff" onChange = { textFunctions . changeTextColor } > </ input >
778830 </ div >
779831 </ div >
780832 < div className = "formatting" >
781833 { /* Text alignment */ }
782834 < div className = "styling" >
783835 < p > Text alignment:</ p >
784836 < div >
785- < button className = "leftAlign" >
837+ < button className = "leftAlign" onClick = { textFunctions . justify } data-justification = "left" >
786838 < i className = "fas fa-align-left" > </ i >
787839 </ button >
788- < button className = "midAlign" >
840+ < button className = "midAlign" onClick = { textFunctions . justify } data-justification = "center" >
789841 < i className = "fas fa-align-center" > </ i >
790842 </ button >
791- < button className = "rightAlign" >
843+ < button className = "rightAlign" onClick = { textFunctions . justify } data-justification = "right" >
792844 < i className = "fas fa-align-right" > </ i >
793845 </ button >
794846 </ div >
@@ -798,14 +850,14 @@ export default function Create() {
798850 < div >
799851 < p > Stroke width:</ p >
800852 < div className = "inputStroke" >
801- < input type = "text" placeholder = "3 " />
853+ < input type = "text" defaultValue = "0 " />
802854 </ div >
803855 </ div >
804856
805857 < div >
806858 < p > Stroke color:</ p >
807859 < div className = "inputStroke" >
808- < input type = "color" defaultValue = "#ffcf4b " />
860+ < input type = "color" defaultValue = "#000000 " />
809861 </ div >
810862 </ div >
811863
@@ -824,7 +876,7 @@ export default function Create() {
824876 </div>
825877 </div> */ }
826878 < div >
827- < ActionButton className = "btn delete" > Reset</ ActionButton >
879+ < ActionButton className = "btn delete" onClick = { deleteSelected } > Reset</ ActionButton >
828880 </ div >
829881 </ Controls >
830882 </ Flex >
@@ -982,15 +1034,16 @@ const EditView = styled.div`
9821034 border: var(--border-dark);
9831035 }
9841036
985- .bold {
986- font-weight: bold ;
1037+ .justify-center {
1038+ text-align: center ;
9871039 }
9881040
989- .italic {
1041+ .justify-left {
1042+ text-align: left;
9901043 }
9911044
992- .underline {
993- text-decoration: underline ;
1045+ .justify-right {
1046+ text-align: right ;
9941047 }
9951048 }
9961049` ;
0 commit comments