Talha912 commited on
Commit
d16f40a
·
verified ·
1 Parent(s): 741660e

Update edit.html

Browse files
Files changed (1) hide show
  1. edit.html +28 -33
edit.html CHANGED
@@ -74,7 +74,7 @@ const FN={sans:['Helvetica','HelveticaBold','HelveticaOblique','HelveticaBoldObl
74
  const MSG={select:'Select words with the mouse, then press Ctrl+C to copy. Double-click selects a word, triple-click a line.',text:'Click any text to edit it. Click empty space to add text. Drag an object to move it.',image:'Choose an image to insert.',cover:'Drag over content to whiteout it.',highlight:'Drag to highlight.',draw:'Draw freely. Click the tool again to stop.',rect:'Drag to draw a rectangle.'};
75
  const COL={highlight:'#ffeb3b',rect:'#2864e8',cover:'#ffffff',draw:'#2864e8'};
76
  const ASC=.945,CF={};
77
- let curPg,tl,clip=null,bytes,doc,page=1,pages=0,zoom=1.25,mode='text',sel=null,editing=null,objs=[],undo=[],W=0,H=0,cv,ov,blocks=[],drawId=0;
78
  const isC=f=>f.startsWith('c:'),BL=f=>CF[f]?CF[f].bl:ASC,uid=()=>Math.random().toString(36).slice(2),cur=()=>objs.find(o=>o.id===sel);
79
  const fcss=o=>isC(o.family)&&CF[o.family]?`"${CF[o.family].name}",${FF[o.fb]||FF.sans}`:FF[o.family]||FF.sans;
80
  const stk=o=>JSON.stringify([o.family,o.size,o.bold,o.italic,o.color]);
@@ -214,12 +214,13 @@ function render(){
214
  if(old){const o=objs.find(x=>x.id===editing);if(o)o.draft=old.innerText.replace(/\n$/,'');old.onblur=null}
215
  ov.innerHTML='';ov.className='overlay '+(mode==='text'?'t':mode==='select'?'s':'c');if(tl)tl.classList.toggle('on',mode==='select');
216
  if(mode==='text')for(const bk of blocks){
217
- const d=document.createElement('div');d.className='blk';d.title='Click to edit · drag to move';
218
- d.style.cssText=`left:${bk.x*zoom}px;top:${(bk.y-ASC*bk.size)*zoom}px;width:${bk.w*zoom}px;height:${bk.size*1.2*zoom}px`;
219
  d.onpointerdown=e=>{
220
- e.stopPropagation();e.preventDefault();
221
  if(e.button!==0)return;
222
- const sx=e.clientX,sy=e.clientY;let moved=false,created=null;
 
 
223
  const mv=ev=>{
224
  const dx=ev.clientX-sx,dy=ev.clientY-sy;
225
  if(!moved&&Math.hypot(dx,dy)>4){
@@ -227,26 +228,36 @@ function render(){
227
  endEdit();
228
  const key=page+':'+Math.round(bk.x)+':'+Math.round(bk.y);
229
  created=objs.find(o=>o.key===key&&o.type==='text');
230
- if(!created){editBlock(bk);created=objs.find(o=>o.key===key&&o.type==='text');editing=null}
231
- if(created){snap();sel=created.id;d.style.opacity='.35'}
 
 
 
 
 
 
 
232
  }
233
  if(moved&&created){
234
- created.x=Math.max(0,bk.x+dx/zoom);
235
- created.y=Math.max(0,bk.y-BL(created.family||fam(bk))*bk.size+dy/zoom);
236
  const item=ov.querySelector('.item[data-id="'+created.id+'"]');
237
  if(item){item.style.left=created.x*zoom+'px';item.style.top=created.y*zoom+'px'}
238
  syncFT();
239
  }
240
  };
241
- const up=ev=>{
242
- removeEventListener('pointermove',mv);removeEventListener('pointerup',up);removeEventListener('pointercancel',up);
243
- d.style.opacity='';
244
- if(moved&&created){render();toast('Text moved. Original position is covered.')}
 
245
  else editBlock(bk);
246
  };
247
- addEventListener('pointermove',mv);addEventListener('pointerup',up);addEventListener('pointercancel',up);
 
 
248
  };
249
- ov.appendChild(d)
250
  }
251
  for(const o of objs.filter(o=>o.page===page)){
252
  const el=document.createElement('div'),s=el.style;el.className='item '+o.type+(sel===o.id?' sel':'');el.dataset.id=o.id;s.left=o.x*zoom+'px';s.top=o.y*zoom+'px';
@@ -319,26 +330,10 @@ document.querySelectorAll('#tb [data-mode]').forEach(b=>b.onclick=()=>setMode(b.
319
  $('imageInput').onchange=e=>{const f=e.target.files[0];if(!f)return;const r=new FileReader();r.onload=()=>{const im=new Image();im.onload=()=>{const k=Math.min(1,220/im.width);add({type:'image',x:40,y:40,w:im.width*k,h:im.height*k,src:r.result})};im.src=r.result};r.readAsDataURL(f);e.target.value=''};
320
  $('undoBtn').onclick=()=>{const s=undo.pop();if(s){objs=JSON.parse(s);sel=editing=null;render()}$('undoBtn').disabled=!undo.length};
321
  addEventListener('keydown',e=>{
322
- const mod=e.ctrlKey||e.metaKey;
323
- if(mod&&e.key.toLowerCase()==='v'&&clip&&doc&&mode==='text'){
324
- e.preventDefault();e.stopPropagation();
325
- const c=JSON.parse(clip);c.id=uid();c.page=page;delete c.key;delete c.orig;delete c.st;
326
- const r=ov.getBoundingClientRect();
327
- if(Number.isFinite(window.lastPointerX)&&Number.isFinite(window.lastPointerY)){
328
- c.x=Math.max(0,(window.lastPointerX-r.left)/zoom);
329
- c.y=Math.max(0,(window.lastPointerY-r.top)/zoom);
330
- }else{c.x+=14;c.y+=14}
331
- snap();objs.push(c);sel=c.id;editing=null;render();toast('Copied object pasted here.');return;
332
- }
333
- if(editing&&!(mod&&['c','x'].includes(e.key.toLowerCase())))return;
334
- if(/INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName))return;
335
  if((e.key==='Delete'||e.key==='Backspace')&&sel){e.preventDefault();del()}
336
- if(mod&&e.key.toLowerCase()==='z'){e.preventDefault();$('undoBtn').click()}
337
- if(mod&&e.key.toLowerCase()==='c'&&sel){
338
- const o=cur();if(o){clip=JSON.stringify(o);e.preventDefault();toast('Object copied. Click the destination, then press Ctrl+V to paste.')}
339
- }
340
  });
341
- document.addEventListener('pointermove',e=>{window.lastPointerX=e.clientX;window.lastPointerY=e.clientY},{passive:true});
342
  const go=n=>{if(n<1||n>pages)return;endEdit();page=n;sel=editing=null;draw()};
343
  $('prevBtn').onclick=()=>go(page-1);$('nextBtn').onclick=()=>go(page+1);
344
  $('zoomIn').onclick=()=>{endEdit();zoom=Math.min(3,zoom+.15);draw()};$('zoomOut').onclick=()=>{endEdit();zoom=Math.max(.5,zoom-.15);draw()};
 
74
  const MSG={select:'Select words with the mouse, then press Ctrl+C to copy. Double-click selects a word, triple-click a line.',text:'Click any text to edit it. Click empty space to add text. Drag an object to move it.',image:'Choose an image to insert.',cover:'Drag over content to whiteout it.',highlight:'Drag to highlight.',draw:'Draw freely. Click the tool again to stop.',rect:'Drag to draw a rectangle.'};
75
  const COL={highlight:'#ffeb3b',rect:'#2864e8',cover:'#ffffff',draw:'#2864e8'};
76
  const ASC=.945,CF={};
77
+ let curPg,tl,bytes,doc,page=1,pages=0,zoom=1.25,mode='text',sel=null,editing=null,objs=[],undo=[],W=0,H=0,cv,ov,blocks=[],drawId=0;
78
  const isC=f=>f.startsWith('c:'),BL=f=>CF[f]?CF[f].bl:ASC,uid=()=>Math.random().toString(36).slice(2),cur=()=>objs.find(o=>o.id===sel);
79
  const fcss=o=>isC(o.family)&&CF[o.family]?`"${CF[o.family].name}",${FF[o.fb]||FF.sans}`:FF[o.family]||FF.sans;
80
  const stk=o=>JSON.stringify([o.family,o.size,o.bold,o.italic,o.color]);
 
214
  if(old){const o=objs.find(x=>x.id===editing);if(o)o.draft=old.innerText.replace(/\n$/,'');old.onblur=null}
215
  ov.innerHTML='';ov.className='overlay '+(mode==='text'?'t':mode==='select'?'s':'c');if(tl)tl.classList.toggle('on',mode==='select');
216
  if(mode==='text')for(const bk of blocks){
217
+ const d=document.createElement('div');d.className='blk';d.title='Click to edit · drag in any direction to move';
218
+ d.style.cssText=`left:${bk.x*zoom}px;top:${(bk.y-ASC*bk.size)*zoom}px;width:${bk.w*zoom}px;height:${bk.size*1.2*zoom}px;touch-action:none`;
219
  d.onpointerdown=e=>{
 
220
  if(e.button!==0)return;
221
+ e.stopPropagation();e.preventDefault();
222
+ const sx=e.clientX,sy=e.clientY;
223
+ let moved=false,created=null,baseX=0,baseY=0;
224
  const mv=ev=>{
225
  const dx=ev.clientX-sx,dy=ev.clientY-sy;
226
  if(!moved&&Math.hypot(dx,dy)>4){
 
228
  endEdit();
229
  const key=page+':'+Math.round(bk.x)+':'+Math.round(bk.y);
230
  created=objs.find(o=>o.key===key&&o.type==='text');
231
+ if(!created){
232
+ editBlock(bk);
233
+ created=objs.find(o=>o.key===key&&o.type==='text');
234
+ editing=null;
235
+ }
236
+ if(created){
237
+ baseX=created.x;baseY=created.y;
238
+ snap();sel=created.id;
239
+ }
240
  }
241
  if(moved&&created){
242
+ created.x=Math.max(0,Math.min(W-2,baseX+dx/zoom));
243
+ created.y=Math.max(0,Math.min(H-created.size,baseY+dy/zoom));
244
  const item=ov.querySelector('.item[data-id="'+created.id+'"]');
245
  if(item){item.style.left=created.x*zoom+'px';item.style.top=created.y*zoom+'px'}
246
  syncFT();
247
  }
248
  };
249
+ const up=()=>{
250
+ removeEventListener('pointermove',mv);
251
+ removeEventListener('pointerup',up);
252
+ removeEventListener('pointercancel',up);
253
+ if(moved&&created){render();toast('Text moved.')}
254
  else editBlock(bk);
255
  };
256
+ addEventListener('pointermove',mv);
257
+ addEventListener('pointerup',up);
258
+ addEventListener('pointercancel',up);
259
  };
260
+ ov.appendChild(d);
261
  }
262
  for(const o of objs.filter(o=>o.page===page)){
263
  const el=document.createElement('div'),s=el.style;el.className='item '+o.type+(sel===o.id?' sel':'');el.dataset.id=o.id;s.left=o.x*zoom+'px';s.top=o.y*zoom+'px';
 
330
  $('imageInput').onchange=e=>{const f=e.target.files[0];if(!f)return;const r=new FileReader();r.onload=()=>{const im=new Image();im.onload=()=>{const k=Math.min(1,220/im.width);add({type:'image',x:40,y:40,w:im.width*k,h:im.height*k,src:r.result})};im.src=r.result};r.readAsDataURL(f);e.target.value=''};
331
  $('undoBtn').onclick=()=>{const s=undo.pop();if(s){objs=JSON.parse(s);sel=editing=null;render()}$('undoBtn').disabled=!undo.length};
332
  addEventListener('keydown',e=>{
333
+ if(/INPUT|SELECT|TEXTAREA/.test(document.activeElement.tagName)||editing)return;
 
 
 
 
 
 
 
 
 
 
 
 
334
  if((e.key==='Delete'||e.key==='Backspace')&&sel){e.preventDefault();del()}
335
+ if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()==='z'){e.preventDefault();$('undoBtn').click()}
 
 
 
336
  });
 
337
  const go=n=>{if(n<1||n>pages)return;endEdit();page=n;sel=editing=null;draw()};
338
  $('prevBtn').onclick=()=>go(page-1);$('nextBtn').onclick=()=>go(page+1);
339
  $('zoomIn').onclick=()=>{endEdit();zoom=Math.min(3,zoom+.15);draw()};$('zoomOut').onclick=()=>{endEdit();zoom=Math.max(.5,zoom-.15);draw()};