Commit ee5d9a4e by ramdayalmunda

loadExternalContent completed

parent c1a38610
...@@ -20,28 +20,18 @@ var ADocEditor = function (customConfig) { ...@@ -20,28 +20,18 @@ var ADocEditor = function (customConfig) {
fontFamily: 'Arial', fontFamily: 'Arial',
bold: false, bold: false,
italic: false, italic: false,
fontColor: "#0007" fontColor: "#1e1f77"
}, },
} }
var config = null; var config = null;
var canvasList = [] var canvasList = []
var dataSet = [ var dataSet = [
{ { id: 1, plainContent: "" }
id: `${++counter}`,
type: "text",
plainContent: "Did you ever hear about the Great Ice Age or the Pleistocene Epoch? Less than one million years ago, in fact, some 12,000 years ago, an ice sheet many thousands of feet thick rode over Burke Mountain in a southeastward direction.",
},
// {
// id: `${++counter}`,
// type: "text",
// plainContent: "Did you ever hear about the Great Ice Age or the Pleistocene Epoch? Less than one million years ago, in fact, some 12,000 years ago, an ice sheet many thousands of feet thick rode over Burke Mountain in a southeastward direction."
// }
] ]
var caretData = { var caretData = {
activeData: dataSet[0], activeData: dataSet[0],
index: dataSet[0].plainContent.length, index: 0,
interval: null, interval: null,
intervalDuration: 800, intervalDuration: 800,
blink: false, blink: false,
...@@ -77,6 +67,7 @@ var ADocEditor = function (customConfig) { ...@@ -77,6 +67,7 @@ var ADocEditor = function (customConfig) {
} }
function reRenderPages(dataList, blinking = false) { function reRenderPages(dataList, blinking = false) {
console.log('rendering', dataList)
if (renderInProgress) return if (renderInProgress) return
renderInProgress = true renderInProgress = true
...@@ -88,15 +79,18 @@ var ADocEditor = function (customConfig) { ...@@ -88,15 +79,18 @@ var ADocEditor = function (customConfig) {
// to clear the canvases // to clear the canvases
for (let i = 0; i < canvasList.length; i++) { for (let i = 0; i < canvasList.length; i++) {
let ctx = canvasList[i].getContext('2d', { willReadFrequently: true }) let ctx = canvasList[i].getContext('2d', { willReadFrequently: true })
ctx.save()
ctx.clearRect(0, 0, canvasList[i].width, canvasList[i].height) ctx.clearRect(0, 0, canvasList[i].width, canvasList[i].height)
ctx.fillStyle = config.format.background ctx.fillStyle = config.format.background
ctx.fillRect(0, 0, canvasList[i].width, canvasList[i].height) ctx.fillRect(0, 0, canvasList[i].width, canvasList[i].height)
ctx.restore()
} }
for (let i = 0; i < dataList.length; i++) { for (let i = 0; i < dataList.length; i++) {
let canvas = canvasList[canvasIndex] let canvas = canvasList[canvasIndex]
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) { if (dataList[i].id == caretData.activeData.id) {
caretData.canvasIndex = canvasIndex; caretData.canvasIndex = canvasIndex;
...@@ -123,16 +117,19 @@ var ADocEditor = function (customConfig) { ...@@ -123,16 +117,19 @@ var ADocEditor = function (customConfig) {
let style = { ...config.style, ...dataSet?.style?.[i] } let style = { ...config.style, ...dataSet?.style?.[i] }
if (i==0){
y += style.fontSize
}
ctx.save() ctx.save()
ctx.font = `28px Arial` ctx.font = `${style.fontSize}px ${style.fontFamily}`
ctx.fillStyle = '#0007' ctx.fillStyle = style.fontColor
let charWidth = ctx.measureText(dataSet.plainContent[i]).width let charWidth = ctx.measureText(dataSet.plainContent[i]).width
ctx.restore() ctx.restore()
dataSet.formatedText[i] = { dataSet.formatedText[i] = {
char: dataSet.plainContent[i], char: dataSet.plainContent[i],
width: charWidth, width: charWidth,
x: x, x: x,
y: y+style.fontSize, y: y,
...style, ...style,
} }
x+= charWidth x+= charWidth
...@@ -154,6 +151,7 @@ var ADocEditor = function (customConfig) { ...@@ -154,6 +151,7 @@ var ADocEditor = function (customConfig) {
for (let i=0; i<dataSet.formatedText.length; i++){ for (let i=0; i<dataSet.formatedText.length; i++){
ctx.save() ctx.save()
ctx.font = `${dataSet.formatedText[i].fontSize}px ${dataSet.formatedText[i].fontFamily}` ctx.font = `${dataSet.formatedText[i].fontSize}px ${dataSet.formatedText[i].fontFamily}`
console.log('dataSet.formatedText[i].fontColor', dataSet.formatedText[i].fontColor)
ctx.fillStyle = dataSet.formatedText[i].fontColor ctx.fillStyle = dataSet.formatedText[i].fontColor
ctx.fillText(dataSet.formatedText[i].char, dataSet.formatedText[i].x, dataSet.formatedText[i].y) ctx.fillText(dataSet.formatedText[i].char, dataSet.formatedText[i].x, dataSet.formatedText[i].y)
ctx.restore() ctx.restore()
...@@ -173,8 +171,10 @@ var ADocEditor = function (customConfig) { ...@@ -173,8 +171,10 @@ var ADocEditor = function (customConfig) {
function renderCaret() { function renderCaret() {
let ctx = canvasList[caretData.canvasIndex].getContext('2d', { willReadFrequently: true }) let ctx = canvasList[caretData.canvasIndex].getContext('2d', { willReadFrequently: true })
let activeTextData = caretData.activeData.formatedText[caretData.index-1] let activeTextData = caretData.activeData.formatedText[caretData.index-1]
let rectX = activeTextData.x+activeTextData.width; let rectX = activeTextData?.x+activeTextData?.width;
let rectY = activeTextData.y-activeTextData.fontSize; let rectY = activeTextData?.y-activeTextData?.fontSize;
rectX = rectX?rectX:0
rectY = rectY?rectY:0
let rectWidth = 2; let rectWidth = 2;
let rectHeight = caretData.caretSize; let rectHeight = caretData.caretSize;
...@@ -206,12 +206,12 @@ var ADocEditor = function (customConfig) { ...@@ -206,12 +206,12 @@ var ADocEditor = function (customConfig) {
function onFocusHandler(e) { function onFocusHandler(e) {
focusedCanvas = e.target focusedCanvas = e.target
caretData.blink = false caretData.blink = false
reRenderPages(dataList) reRenderPages(dataSet)
} }
function onBlurHandler(e) { function onBlurHandler(e) {
focusedCanvas = null focusedCanvas = null
caretData.blink = true caretData.blink = true
reRenderPages(dataList) reRenderPages(dataSet)
} }
...@@ -255,7 +255,7 @@ var ADocEditor = function (customConfig) { ...@@ -255,7 +255,7 @@ var ADocEditor = function (customConfig) {
else if (e.keyCode == 40) { }// down key else if (e.keyCode == 40) { }// down key
else { } // unhandled cases else { } // unhandled cases
reRenderPages(dataList) reRenderPages(dataSet)
clearInterval(caretData.interval) clearInterval(caretData.interval)
caretData.interval = setInterval(() => { caretData.interval = setInterval(() => {
caretData.blink = !caretData.blink caretData.blink = !caretData.blink
...@@ -276,9 +276,20 @@ var ADocEditor = function (customConfig) { ...@@ -276,9 +276,20 @@ var ADocEditor = function (customConfig) {
function destory() { function destory() {
clearInterval(caretData.interval) clearInterval(caretData.interval)
} }
return {
var returnObj = {
destory, destory,
loadContent: function(data){
console.log('to load', JSON.parse( JSON.stringify( dataSet)))
dataSet = JSON.parse( JSON.stringify(data) )
console.log('new Data', JSON.parse( JSON.stringify( dataSet)))
caretData.activeData = null
caretData.activeData = dataSet[0]
caretData.index = dataSet[0].plainContent.length
reRenderPages(dataSet)
},
} }
return returnObj
} }
......
import { tempDocData } from "./randomDataSet.js" import { tempDocData } from "./randomDataSet.js"
!(async function(){ !(async function(){
let editor = new ADocEditor({ element: document.getElementById("user-container-for-editor") }) let editor = new ADocEditor({ element: document.getElementById("user-container-for-editor") })
editor.loadContent(tempDocData)
})() })()
\ No newline at end of file
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