Commit a91248d9 by ramdayalmunda

setting up new data

parent 644d57ce
...@@ -3,8 +3,6 @@ var ADocEditor = function (customConfig) { ...@@ -3,8 +3,6 @@ var ADocEditor = function (customConfig) {
var counter = 0 var counter = 0
var container = null var container = null
var mainComponent = null var mainComponent = null
var headerComponent = null
var footerComponent = null
var scrollingComponent = null var scrollingComponent = null
var leftSidebar = null var leftSidebar = null
var rightSidebar = null var rightSidebar = null
...@@ -12,7 +10,7 @@ var ADocEditor = function (customConfig) { ...@@ -12,7 +10,7 @@ var ADocEditor = function (customConfig) {
element: "", element: "",
pageSetup: { pageSetup: {
width: 210, width: 210,
height: 210, height: 140,
}, },
format: { format: {
background: "#fff", background: "#fff",
...@@ -25,10 +23,10 @@ var ADocEditor = function (customConfig) { ...@@ -25,10 +23,10 @@ var ADocEditor = function (customConfig) {
fontFamily: 'Arial', fontFamily: 'Arial',
bold: false, bold: false,
italic: false, italic: false,
fontColor: "#1e1f77" fontColor: "#0004"
}, },
} }
var config = JSON.parse( JSON.stringify( defaultConfig ) ); var config = JSON.parse(JSON.stringify(defaultConfig));
var canvasList = [] var canvasList = []
const dataTypes = [ const dataTypes = [
"paragraph", // simple text filled line by line "paragraph", // simple text filled line by line
...@@ -43,6 +41,8 @@ var ADocEditor = function (customConfig) { ...@@ -43,6 +41,8 @@ var ADocEditor = function (customConfig) {
} }
] ]
var lines = []
var caretData = { var caretData = {
activeData: dataSet[0], activeData: dataSet[0],
index: 0, index: 0,
...@@ -51,8 +51,8 @@ var ADocEditor = function (customConfig) { ...@@ -51,8 +51,8 @@ var ADocEditor = function (customConfig) {
blink: false, blink: false,
canvasIndex: 0, canvasIndex: 0,
caretSize: config.style.fontSize, caretSize: config.style.fontSize,
x: config.style.margin, x: config.format.margin,
y: 3*config.style.fontSize/4, y: config.format.margin + (3 * config.style.fontSize / 4),
} }
var renderInProgress = false; var renderInProgress = false;
...@@ -128,23 +128,17 @@ var ADocEditor = function (customConfig) { ...@@ -128,23 +128,17 @@ var ADocEditor = function (customConfig) {
footerComponent = document.getElementById(footerComponentId) footerComponent = document.getElementById(footerComponentId)
leftSidebar = document.getElementById(leftSidebarId) leftSidebar = document.getElementById(leftSidebarId)
rightSidebar = document.getElementById(rightSidebarId) rightSidebar = document.getElementById(rightSidebarId)
mainComponent.getElementsByClassName('toggle-sidebar')[0].addEventListener('click',(e)=>{ mainComponent.getElementsByClassName('toggle-sidebar')[0].addEventListener('click', (e) => {
leftSidebar.classList.toggle('hide') leftSidebar.classList.toggle('hide')
}) })
mainComponent.getElementsByClassName('toggle-sidebar')[1].addEventListener('click',(e)=>{ mainComponent.getElementsByClassName('toggle-sidebar')[1].addEventListener('click', (e) => {
rightSidebar.classList.toggle('hide') rightSidebar.classList.toggle('hide')
}) })
// mainComponent = document.createElement('div')
// mainComponent.setAttribute('class', 'a-doc-editor')
// container.append(mainComponent)
reRenderPages(dataSet) reRenderPages(dataSet)
} }
function reRenderPages(dataList, blinking = false) { function reRenderPages(dataList) {
if (renderInProgress) return if (renderInProgress) return
renderInProgress = true renderInProgress = true
...@@ -157,7 +151,7 @@ var ADocEditor = function (customConfig) { ...@@ -157,7 +151,7 @@ var ADocEditor = function (customConfig) {
for (let i = 0; i < canvasList.length; i++) { for (let i = 0; i < canvasList.length; i++) {
let ctx = canvasList[i].el.getContext('2d', { willReadFrequently: true }) let ctx = canvasList[i].el.getContext('2d', { willReadFrequently: true })
ctx.save() ctx.save()
ctx.clearRect(0, 0, canvasList[i].el.width, canvasList[i].el.height) ctx.clearRect(0, 0, canvasList[i].el.width, canvasList[i].el.height); // clears the canvas
ctx.fillStyle = config.format.background ctx.fillStyle = config.format.background
ctx.fillRect(0, 0, canvasList[i].el.width, canvasList[i].el.height) ctx.fillRect(0, 0, canvasList[i].el.width, canvasList[i].el.height)
ctx.restore() ctx.restore()
...@@ -167,73 +161,15 @@ var ADocEditor = function (customConfig) { ...@@ -167,73 +161,15 @@ var ADocEditor = function (customConfig) {
let canvas = canvasList[canvasIndex].el let canvas = canvasList[canvasIndex].el
canvas.setAttribute("tabIndex", -1) canvas.setAttribute("tabIndex", -1)
renderText(canvas, dataList[i]) renderText(canvas, dataList[i])
x = 0;
if (dataList[i].id == caretData.activeData.id) {
caretData.canvasIndex = canvasIndex;
}
} }
function renderText(canvas, dataSet) { function renderText(canvas, dataBlock) {
console.log('to render text', dataBlock)
let ctx = canvas.getContext('2d', { willReadFrequently: true }) let ctx = canvas.getContext('2d', { willReadFrequently: true })
ctx.save() ctx.save()
ctx.font = `${config.style.fontSize}px ${config.style.fontFamily}`; ctx.fillStyle = "#0006"; ctx.fillText("A", x,y+config.style.fontSize)
let maxWidth = config.pageSetup.canvasWidth // to calculate the lines
let wordStartIndex = 0
let wordEndIndex = 0
let wordsLength = 0;
dataSet.formatedText = []
for (let i = 0; i < dataSet.plainContent.length; i++) {
if (/\s+/.test(dataSet.plainContent[i])) {
wordEndIndex = i
}
let style = { ...config.style, ...dataSet?.style?.[i] }
if (i == 0) {
y += style.fontSize
}
ctx.save()
ctx.font = `${style.fontSize}px ${style.fontFamily}`
ctx.fillStyle = style.fontColor
let charWidth = ctx.measureText(dataSet.plainContent[i]).width
ctx.restore()
dataSet.formatedText[i] = {
char: dataSet.plainContent[i],
width: charWidth,
x: x,
y: y,
canvas: canvas,
...style,
}
x += charWidth
if (wordsLength + charWidth <= maxWidth) {
wordsLength += charWidth
} else {
i = wordEndIndex
wordsLength = 0
y += style.fontSize
x = 0;
wordStartIndex = wordEndIndex
}
}
for (let i = 0; i < dataSet.formatedText.length; i++) {
ctx.save()
ctx.font = `${dataSet.formatedText[i].fontSize}px ${dataSet.formatedText[i].fontFamily}`
ctx.fillStyle = dataSet.formatedText[i].fontColor
ctx.fillText(dataSet.formatedText[i].char, dataSet.formatedText[i].x, dataSet.formatedText[i].y)
ctx.restore()
}
ctx.restore() ctx.restore()
...@@ -244,15 +180,13 @@ var ADocEditor = function (customConfig) { ...@@ -244,15 +180,13 @@ var ADocEditor = function (customConfig) {
// to render caret // to render caret
function renderCaret() { function renderCaret() {
let ctx = canvasList[caretData.canvasIndex].el.getContext('2d', { willReadFrequently: true }) let ctx = canvasList[caretData.canvasIndex].el.getContext('2d', { willReadFrequently: true })
let activeTextData = caretData.activeData.formatedText[caretData.index - 1] ctx.save()
if (!activeTextData) activeTextData = caretData.activeData.style let rectX = 0, rectY = 0, rectWidth = 2, rectHeight = 5*config.style.fontSize/4;
let rectX = activeTextData?.x + activeTextData?.width; if (caretData.caretIndex == 0) {
let rectY = activeTextData?.y - activeTextData?.fontSize; // pick style from line
rectX = rectX ? rectX : 0 } else {
rectY = rectY ? rectY : 0 // pick style for previous character
let rectWidth = 2; }
let rectHeight = caretData.caretSize + activeTextData?.fontSize/3;
const imageData = ctx.getImageData(rectX, rectY, rectWidth, rectHeight); const imageData = ctx.getImageData(rectX, rectY, rectWidth, rectHeight);
const data = imageData.data; const data = imageData.data;
...@@ -264,6 +198,8 @@ var ADocEditor = function (customConfig) { ...@@ -264,6 +198,8 @@ var ADocEditor = function (customConfig) {
// Alpha channel remains unchanged (data[i + 3]) // Alpha channel remains unchanged (data[i + 3])
ctx.putImageData(imageData, rectX, rectY); ctx.putImageData(imageData, rectX, rectY);
} }
ctx.restore()
} }
function createCanvas() { function createCanvas() {
...@@ -283,10 +219,6 @@ var ADocEditor = function (customConfig) { ...@@ -283,10 +219,6 @@ var ADocEditor = function (customConfig) {
focusedCanvas = e.target focusedCanvas = e.target
caretData.blink = false caretData.blink = false
reRenderPages(dataSet) reRenderPages(dataSet)
caretData.interval = setInterval(() => {
caretData.blink = !caretData.blink
reRenderPages(dataSet, true)
}, caretData.intervalDuration)
} }
function onBlurHandler(e) { function onBlurHandler(e) {
focusedCanvas = null focusedCanvas = null
...@@ -315,13 +247,13 @@ var ADocEditor = function (customConfig) { ...@@ -315,13 +247,13 @@ var ADocEditor = function (customConfig) {
e.preventDefault() e.preventDefault()
} }
else if (e.keyCode == 13) { // Enter Key else if (e.keyCode == 13) { // Enter Key
if (caretData){ if (caretData) {
if (caretData.activeData.type == 0){ // for plain text // just go to next line if (caretData.activeData.type == 0) { // for plain text // just go to next line
let newLineData = { let newLineData = {
id: ++counter, id: ++counter,
type: 0, type: 0,
plainContent: "", plainContent: "",
style: JSON.parse( JSON.stringify(caretData.activeData.formatedText[caretData.activeData.formatedText.length-1]) ) style: JSON.parse(JSON.stringify(caretData.activeData.formatedText[caretData.activeData.formatedText.length - 1]))
} }
newLineData.style.y += newLineData.style.fontSize newLineData.style.y += newLineData.style.fontSize
newLineData.style.x = 0 newLineData.style.x = 0
...@@ -343,14 +275,14 @@ var ADocEditor = function (customConfig) { ...@@ -343,14 +275,14 @@ var ADocEditor = function (customConfig) {
} }
else if (e.keyCode == 37) { // left key else if (e.keyCode == 37) { // left key
e.preventDefault() e.preventDefault()
if (caretData.index<=0){ if (caretData.index <= 0) {
let currentIndex = dataSet.findIndex( item => item.id == caretData.activeData.id ) let currentIndex = dataSet.findIndex(item => item.id == caretData.activeData.id)
let previousData = dataSet[currentIndex-1] let previousData = dataSet[currentIndex - 1]
if (previousData){ if (previousData) {
caretData.activeData = previousData caretData.activeData = previousData
caretData.index = previousData.plainContent.length caretData.index = previousData.plainContent.length
} }
}else{ } else {
caretData.index = (caretData.index <= 0) ? 0 : caretData.index - 1 caretData.index = (caretData.index <= 0) ? 0 : caretData.index - 1
} }
} }
...@@ -359,12 +291,12 @@ var ADocEditor = function (customConfig) { ...@@ -359,12 +291,12 @@ var ADocEditor = function (customConfig) {
} }
else if (e.keyCode == 39) { // right key else if (e.keyCode == 39) { // right key
e.preventDefault() e.preventDefault()
if (caretData.index <= caretData.activeData.plainContent.length-1){ if (caretData.index <= caretData.activeData.plainContent.length - 1) {
caretData.index = (caretData.index >= caretData.activeData.plainContent.length) ? caretData.activeData.plainContent.length : caretData.index + 1 caretData.index = (caretData.index >= caretData.activeData.plainContent.length) ? caretData.activeData.plainContent.length : caretData.index + 1
}else{ } else {
let currentIndex = dataSet.findIndex( item => item.id == caretData.activeData.id ) let currentIndex = dataSet.findIndex(item => item.id == caretData.activeData.id)
let nextData = dataSet[currentIndex+1] let nextData = dataSet[currentIndex + 1]
if (nextData){ if (nextData) {
caretData.index = 0 caretData.index = 0
caretData.activeData = nextData caretData.activeData = nextData
} }
...@@ -378,33 +310,21 @@ var ADocEditor = function (customConfig) { ...@@ -378,33 +310,21 @@ var ADocEditor = function (customConfig) {
reRenderPages(dataSet) reRenderPages(dataSet)
clearInterval(caretData.interval) clearInterval(caretData.interval)
caretData.interval = setInterval(() => {
caretData.blink = !caretData.blink
reRenderPages(dataSet, true)
}, caretData.intervalDuration)
} }
function setCaretPosition(e){ function setCaretPosition(e) {
console.log('to set caret position, on click')
let rect = e.target.getBoundingClientRect() let rect = e.target.getBoundingClientRect()
// console.log('rect', rect) // console.log('rect', rect)
let position = { let position = {
xP: e.offsetX/rect.width, xP: e.offsetX / rect.width,
yP: e.offsetY/rect.height, yP: e.offsetY / rect.height,
} }
let canvasData = canvasList.find( item => item.el == e.target ) let canvasData = canvasList.find(item => item.el == e.target)
position.x = position.xP*config.pageSetup.canvasWidth position.x = position.xP * config.pageSetup.canvasWidth
position.y = position.yP*config.pageSetup.canvasHeight position.y = position.yP * config.pageSetup.canvasHeight
let lineY = dataSet[canvasData.dataIndex].style.y let lineY = dataSet[canvasData.dataIndex].style.y
console.log('positionY', position.y, dataSet[canvasData.dataIndex].style.fontSize) console.log('positionY', position.y, dataSet[canvasData.dataIndex].style.fontSize)
for (let i=canvasData.dataIndex; i<dataSet.length; i++){
for ( let j=0; j< dataSet[i].formatedText.length; j++ ){
if ( Math.floor( dataSet[i].formatedText[j].x ) == Math.floor(position.x) ){
console.log('breaked')
break;
}
}
}
} }
...@@ -430,8 +350,8 @@ var ADocEditor = function (customConfig) { ...@@ -430,8 +350,8 @@ var ADocEditor = function (customConfig) {
caretData.activeData = null caretData.activeData = null
caretData.activeData = dataSet[0] caretData.activeData = dataSet[0]
}, },
getContent: function (){ getContent: function () {
return JSON.parse( JSON.stringify( dataSet ) ) return JSON.parse(JSON.stringify(dataSet))
} }
} }
return returnObj return returnObj
......
...@@ -14,7 +14,6 @@ var defaultConfig = { ...@@ -14,7 +14,6 @@ var defaultConfig = {
format: { format: {
background: "#fff", background: "#fff",
margin: 20, // mm // margin: 20, // mm //
canvasMargin: 20, // px //
border: "", // could be image or anything border: "", // could be image or anything
}, },
style: { style: {
...@@ -26,6 +25,16 @@ var defaultConfig = { ...@@ -26,6 +25,16 @@ var defaultConfig = {
}, },
} }
var caretData = {
activeData: dataSet[0], // this holds the active data from the dataSet
index: 0,// this holds the caret index of the character in that activeData
interval: null, // to store the setInterval for the blinking cursor animation
intervalDuration: 800, // ms // to detemine for how long the caret is blinked before reappearing again
blink: false, // this will keep alternating to to show the caret or not
canvasIndex: 0, // to store index of the canvas which isBeing edited right now.
caretSize: config.style.fontSize,
}
var dataType = [ var dataType = [
"plainText", // plain text, normal text in a pdf or doc text "plainText", // plain text, normal text in a pdf or doc text
"list", // items displayed in a list format "list", // items displayed in a list format
...@@ -40,28 +49,16 @@ var dataSet = [ ...@@ -40,28 +49,16 @@ var dataSet = [
}, },
{ {
type: 1, // type 1, denotes it is a list, type: 1, // type 1, denotes it is a list,
listType: "",
tabNumber: 0, // tab number by default is zero
plainText: "This is the first item in the list. it can be wrapped around the line if necessary"
},
{
// this is the second item on the list because the prvious item is also a list and has the same tabNumber
type: 1, // type 1, denotes it is a list,
itemType: "", itemType: "",
tabNumber: 0, // tab number by default is zero tabNumber: 0, // tab number by default is zero
items: [ plainText: "The second item on the list is here."
{
plainText: "This is the first item in the list. it can be wrapped around the line if necessary"
},
{
plainText: "Second item is here."
},
{
plainText: "The list can be nested like this",
items: [
{
plainText: "First nested item is here"
},
{
plainText: "This is the second nested item"
}
]
}
]
}, },
{ {
type: 0, // this is just a blank line that is added type: 0, // this is just a blank line that is added
......
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