Commit 4196195b by ramdayalmunda

place caret on clicking

parent 9cf6d76d
...@@ -14,7 +14,7 @@ var ADocEditor = function (customConfig) { ...@@ -14,7 +14,7 @@ var ADocEditor = function (customConfig) {
tabWidth: 20, // mm tabWidth: 20, // mm
}, },
style: { style: {
fontSize: 20, // this is in mm fontSize: 16, // this is in mm
fontFamily: 'Calibri', fontFamily: 'Calibri',
bold: false, bold: false,
italic: false, italic: false,
...@@ -39,7 +39,7 @@ var ADocEditor = function (customConfig) { ...@@ -39,7 +39,7 @@ var ADocEditor = function (customConfig) {
index: 0, index: 0,
timeout: null, timeout: null,
timeoutDuration: 800, timeoutDuration: 800,
blink: false, blink: true,
pageIndex: 0, pageIndex: 0,
caretSize: config.style.fontSize, caretSize: config.style.fontSize,
previousCaret: null, previousCaret: null,
...@@ -142,6 +142,7 @@ var ADocEditor = function (customConfig) { ...@@ -142,6 +142,7 @@ var ADocEditor = function (customConfig) {
reConfigure(customConfig) reConfigure(customConfig)
reRenderCanvas() reRenderCanvas()
addGlobalEvents()
} }
function reConfigure(newConfig) { function reConfigure(newConfig) {
if (newConfig?.size && paperSizes[newConfig?.size]) { config.pageSetup = { ...paperSizes[newConfig.size], size: newConfig.size } } if (newConfig?.size && paperSizes[newConfig?.size]) { config.pageSetup = { ...paperSizes[newConfig.size], size: newConfig.size } }
...@@ -400,7 +401,6 @@ var ADocEditor = function (customConfig) { ...@@ -400,7 +401,6 @@ var ADocEditor = function (customConfig) {
} }
function keydownHandler(e) { function keydownHandler(e) {
console.clear()
if (e.altKey) return if (e.altKey) return
else if (e.key == 'Backspace') { else if (e.key == 'Backspace') {
if (caretData.index == 0) { if (caretData.index == 0) {
...@@ -459,22 +459,52 @@ var ADocEditor = function (customConfig) { ...@@ -459,22 +459,52 @@ var ADocEditor = function (customConfig) {
} }
caretData.blink = false caretData.blink = false
caretData.previousCaret = null caretData.previousCaret = null
reRenderCanvas(dataList) reRenderCanvas()
}
function placeCaret( cursor={ x: 0, y: 0 } ){
let found = false
for( let l=0; l<lines.length; l++ ){
if ( (cursor.y <= lines[l].y) && (cursor.y>= (lines[l].maxFontSize*pxMmRatio)) ){
let dataSet = dataList[ lines[l].dataIndex ]
for ( let c=lines[l].charStartIndex; c<=lines[l].charEndIndex; c++ ){
if ( (cursor.x >= dataSet.formatedText[c].x) && (cursor.x<= (dataSet.formatedText[c].width+dataSet.formatedText[c].x))){
caretData.activeData = dataSet
if ( cursor.x <= dataSet.formatedText[c].x+dataSet.formatedText[c].width/2 ){
caretData.index = c
}else{
caretData.index = c+1
}
found = true
caretData.blink = false
renderCaret(true)
return
}
}
if ( found )break; // break only when the caret index is determined
}
}
if (!found){
console.log('no char found')
}
} }
function mousedownHandler(e) { function mousedownHandler(e) {
console.clear()
if (focussedPage) { if (focussedPage) {
console.log('mouse down', e.target)
const rect = e.target.getBoundingClientRect(); // Get the position of the canvas const rect = e.target.getBoundingClientRect(); // Get the position of the canvas
const x = e.clientX - rect.left; // Adjusted X coordinate // const x = e.clientX - rect.left; // Adjusted X coordinate
const y = e.clientY - rect.top; // Adjusted Y coordinate // const y = e.clientY - rect.top; // Adjusted Y coordinate
const width = Math.round(rect.width) // const width = Math.round(rect.width)
const height = Math.round(rect.height) // const height = Math.round(rect.height)
console.log(`${x} ${y} -- ${width} ${height}`); const pxX = (e.clientX - rect.left)*config.pageSetup.pxWidth/rect.width;
console.log(dataList) const pxY = (e.clientY - rect.top)*config.pageSetup.pxHeight/rect.height;
console.log(lines) placeCaret({ x:pxX, y: pxY })
} }
} }
function onFocusHandler(e) { function onFocusHandler(e) {
...@@ -543,6 +573,14 @@ var ADocEditor = function (customConfig) { ...@@ -543,6 +573,14 @@ var ADocEditor = function (customConfig) {
} }
function onMouseWheelHandler(e){
if (e.ctrlKey || e.metaKey) e.preventDefault()
}
function addGlobalEvents(e){
shadow.querySelector('.page-list').addEventListener('wheel', onMouseWheelHandler)
}
var returnObj = { var returnObj = {
addFonts, addFonts,
getConfiguration() { return JSON.parse(JSON.stringify(config)) }, getConfiguration() { return JSON.parse(JSON.stringify(config)) },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment