Commit 940f5e70 by ramdayalmunda

fixes like cursor, e nter, backspace, width, numbering count and font size

parent 632d63be
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
justify-content: flex-start; justify-content: flex-start;
} }
hidden {
display: none;
}
.header { .header {
position: relative; position: relative;
display: block; display: block;
...@@ -21,7 +25,31 @@ ...@@ -21,7 +25,31 @@
width: 100%; width: 100%;
background: pink; background: pink;
display: flex; display: flex;
gap: 10px; }
.toolbar [adc-type="menu-options"] {
position: absolute;
opacity: 0;
pointer-events: none;
border-radius: 5px;
background-color: goldenrod;
}
.toolbar [adc-type="menu-item"]:hover [adc-type="menu-options"] {
opacity: 1;
z-index: 10;
pointer-events: auto;
}
.toolbar [adc-type="menu-options"] [adc-type="sub-menu"] {
border-radius: 5px;
min-width: 100px;
padding: 5px;
background-color: goldenrod;
}
.toolbar [adc-type="menu-options"] [adc-type="sub-menu"]:hover {
background-color: gold;
} }
.toolbar .item { .toolbar .item {
...@@ -36,7 +64,8 @@ ...@@ -36,7 +64,8 @@
margin: 4px; margin: 4px;
cursor: pointer; cursor: pointer;
} }
.popover{
.popover {
position: absolute; position: absolute;
padding: 5px; padding: 5px;
background-color: gray; background-color: gray;
...@@ -44,32 +73,40 @@ ...@@ -44,32 +73,40 @@
opacity: 0; opacity: 0;
pointer-events: none; pointer-events: none;
} }
.popover.show{
.popover.show {
opacity: 1; opacity: 1;
pointer-events: auto; pointer-events: auto;
z-index: 10; z-index: 10;
} }
.toolbar .item:hover { .toolbar .item:hover {
background: #5fad5f; background: #5fad5f;
} }
[adc-type="popover"] { [adc-type="popover"] {
pointer-events: none; pointer-events: none;
} }
.small-input { .small-input {
width: 50px; width: 50px;
} }
.small-btn { .small-btn {
width: 25px; width: 25px;
} }
.option { .option {
border: 1px solid black; border: 1px solid black;
padding: 5px; padding: 5px;
margin: 5px; margin: 5px;
border-radius: 4px; border-radius: 4px;
} }
.option:hover { .option:hover {
background-color: #f5c468; background-color: #f5c468;
} }
.page-list { .page-list {
position: relative; position: relative;
display: block; display: block;
......
...@@ -5,8 +5,8 @@ var ADocEditor = function (customConfig) { ...@@ -5,8 +5,8 @@ var ADocEditor = function (customConfig) {
var paperSizes = { var paperSizes = {
"A4": { mmWidth: 210, mmHeight: 297 }, "A4": { mmWidth: 210, mmHeight: 297 },
} }
const letters = { "0": "a","1": "b","2": "c","3": "d","4": "e","5": "f","6": "g","7": "h","8": "i","9": "j","a": "k","b": "l","c": "m","d": "n","e": "o","f": "p","g": "q","h": "r","i": "s","j": "t","k": "u","l": "v","m": "w","n": "x","o": "y","p": "z" } const letters = { "0": "a", "1": "b", "2": "c", "3": "d", "4": "e", "5": "f", "6": "g", "7": "h", "8": "i", "9": "j", "a": "k", "b": "l", "c": "m", "d": "n", "e": "o", "f": "p", "g": "q", "h": "r", "i": "s", "j": "t", "k": "u", "l": "v", "m": "w", "n": "x", "o": "y", "p": "z" }
const romanNumerals = [ ["I", "IV", "V", "IX"], ["X", "XL", "L", "XC"], ["C", "CD", "D", "CM"], ["M"] ]; const romanNumerals = [["I", "IV", "V", "IX"], ["X", "XL", "L", "XC"], ["C", "CD", "D", "CM"], ["M"]];
var defaultConfig = { var defaultConfig = {
pageSetup: { size: "A4" }, zoom: 1, pageSetup: { size: "A4" }, zoom: 1,
format: { format: {
...@@ -60,6 +60,25 @@ var ADocEditor = function (customConfig) { ...@@ -60,6 +60,25 @@ var ADocEditor = function (customConfig) {
htmlStr = /*html*/` htmlStr = /*html*/`
<div class="header"> <div class="header">
<div class="toolbar">
<div class="item" adc-target="file-menu" adc-type="menu-item">
<span>File</span>
<div adc="file-menu" adc-type="menu-options">
<div adc-type="sub-menu" adc-action="new-file">New</div>
<div adc-type="sub-menu" adc-action="open-file">Open</div>
<div adc-type="sub-menu" adc-action="save-file">Save</div>
</div>
</div>
<div class="item" adc-target="help-menu" adc-type="menu-item">
<span>Help</span>
<div adc="help-menu" adc-type="menu-options">
<div adc-type="sub-menu" adc-action="about">About</div>
<div adc-type="sub-menu" adc-action="tutorial">Tutorial</div>
</div>
</div>
</div>
<div class="toolbar"> <div class="toolbar">
<select class="item" adc="font-select"></select> <select class="item" adc="font-select"></select>
<div class="item" adc="list-handler" adc-target="list-popover"> <div class="item" adc="list-handler" adc-target="list-popover">
...@@ -102,6 +121,10 @@ var ADocEditor = function (customConfig) { ...@@ -102,6 +121,10 @@ var ADocEditor = function (customConfig) {
<span class="item">Pages : 1</span> <span class="item">Pages : 1</span>
</div> </div>
</div> </div>
<hidden>
<a adc="download-link">
<input adc="open-file" type="file" accept=".json"/>
</hidden>
`; `;
...@@ -155,13 +178,13 @@ var ADocEditor = function (customConfig) { ...@@ -155,13 +178,13 @@ var ADocEditor = function (customConfig) {
!(function handleList() { !(function handleList() {
let listItems = shadow.querySelectorAll('[adc-toggle="listing-option"]') let listItems = shadow.querySelectorAll('[adc-toggle="listing-option"]')
let popover = shadow.querySelector('[adc-type="popover"]') let popover = shadow.querySelector('[adc-type="popover"]')
for(let i=0; i<listItems.length; i++){ for (let i = 0; i < listItems.length; i++) {
listItems[i].addEventListener('click', (e) => { listItems[i].addEventListener('click', (e) => {
let value = Number(e.target.getAttribute('value')) let value = Number(e.target.getAttribute('value'))
value = value?value:0 value = value ? value : 0
if (caretData.activeData) { if (caretData.activeData) {
caretData.activeData.type = (caretData.activeData.type == 1 && caretData.activeData.listStyle==value) ? 0 : 1 caretData.activeData.type = (caretData.activeData.type == 1 && caretData.activeData.listStyle == value) ? 0 : 1
if (caretData.activeData.type==1){ if (caretData.activeData.type == 1) {
caretData.activeData.listStyle = value caretData.activeData.listStyle = value
} }
changeListStyle() changeListStyle()
...@@ -175,25 +198,25 @@ var ADocEditor = function (customConfig) { ...@@ -175,25 +198,25 @@ var ADocEditor = function (customConfig) {
} }
})() })()
!(function fontSizeHandler(){ !(function fontSizeHandler() {
let fontSizeInput = shadow.querySelector('[adc="font-size-input"]') let fontSizeInput = shadow.querySelector('[adc="font-size-input"]')
fontSizeInput.value = caretData.style.fontSize fontSizeInput.value = caretData.style.fontSize
let fontSizeChangers = shadow.querySelectorAll('[adc="font-size-change"]') let fontSizeChangers = shadow.querySelectorAll('[adc="font-size-change"]')
fontSizeChangers.forEach( btn => { fontSizeChangers.forEach(btn => {
btn.addEventListener( 'click',(e)=>{ btn.addEventListener('click', (e) => {
let change = Number(e.target.getAttribute('value')) let change = Number(e.target.getAttribute('value'))
change = change?change:0 change = change ? change : 0
change = caretData.style.fontSize + change change = caretData.style.fontSize + change
change = change?change:config.style.fontSize change = change ? change : config.style.fontSize
caretData.style.fontSize = change caretData.style.fontSize = change
fontSizeInput.value = change fontSizeInput.value = change
focusOnPage() focusOnPage()
} ) })
} ) })
function changeFontEvent(e){ function changeFontEvent(e) {
let value = Number( e.target.value ) let value = Number(e.target.value)
value = value?value:config.style.fontSize value = value ? value : config.style.fontSize
caretData.style.fontSize = value caretData.style.fontSize = value
if (e?.key == 'Enter') focusOnPage() if (e?.key == 'Enter') focusOnPage()
} }
...@@ -202,6 +225,30 @@ var ADocEditor = function (customConfig) { ...@@ -202,6 +225,30 @@ var ADocEditor = function (customConfig) {
})() })()
!(function menuHandler() {
shadow.querySelector('[adc-action="new-file"]')
.addEventListener('click', newDocument)
shadow.querySelector('[adc-action="save-file"]')
.addEventListener('click', downloadJSON)
let openFileInput = shadow.querySelector('[adc="open-file"]')
openFileInput.addEventListener('change', (e) => {
let file = e.target?.files?.[0]
if (file) {
const reader = new FileReader()
reader.onload = function (e) {
const fileContent = e.target.result;
let jsonData = JSON.parse(fileContent)
setDocumentData(jsonData)
};
reader.readAsText(file);
}
})
shadow.querySelector('[adc-action="open-file"]')
.addEventListener('click', () => { openFileInput.click() })
})()
reConfigure(customConfig) reConfigure(customConfig)
reRenderCanvas() reRenderCanvas()
addGlobalEvents() addGlobalEvents()
...@@ -313,7 +360,7 @@ var ADocEditor = function (customConfig) { ...@@ -313,7 +360,7 @@ var ADocEditor = function (customConfig) {
} }
for (c = 0; c < dataBlock.plainContent.length; c++) { for (c = 0; c < dataBlock.plainContent.length; c++) {
let style = dataBlock?.formatedText?.[c] let style = dataBlock?.formatedText?.[c]
lineObj.maxFontSize = (lineObj.maxFontSize<dataBlock?.formatedText?.[c].fontSize)?dataBlock?.formatedText?.[c].fontSize:lineObj.maxFontSize lineObj.maxFontSize = (lineObj.maxFontSize < dataBlock?.formatedText?.[c].fontSize) ? dataBlock?.formatedText?.[c].fontSize : lineObj.maxFontSize
if (/\s/.test(dataBlock.plainContent[c])) { if (/\s/.test(dataBlock.plainContent[c])) {
// condition to check if a blank character is found. // condition to check if a blank character is found.
...@@ -346,10 +393,10 @@ var ADocEditor = function (customConfig) { ...@@ -346,10 +393,10 @@ var ADocEditor = function (customConfig) {
lineObj = new getLineObj() lineObj = new getLineObj()
lineObj.maxLineWidth = maxLineWidth lineObj.maxLineWidth = maxLineWidth
lineObj.maxFontSize = style.fontSize lineObj.maxFontSize = style.fontSize
if (dataBlock.type == 1){ if (dataBlock.type == 1) {
lineObj.tabWidth = tabWidth lineObj.tabWidth = tabWidth
lineObj.tabCount = dataBlock.tabCount lineObj.tabCount = dataBlock.tabCount
}else{ } else {
maxLineWidth = config.pageSetup.pxWidth - (config.format.margin * pxMmRatio * 2) maxLineWidth = config.pageSetup.pxWidth - (config.format.margin * pxMmRatio * 2)
lineObj.tabWidth = 0 lineObj.tabWidth = 0
lineObj.tabCount = 0 lineObj.tabCount = 0
...@@ -444,9 +491,9 @@ var ADocEditor = function (customConfig) { ...@@ -444,9 +491,9 @@ var ADocEditor = function (customConfig) {
function convertToLetter(num) { function convertToLetter(num) {
num = (num-1).toString(26) num = (num - 1).toString(26)
let label = "" let label = ""
for(let i=0;i<num.length;i++) label+= letters[num[i]] for (let i = 0; i < num.length; i++) label += letters[num[i]]
return label return label
} }
...@@ -535,9 +582,9 @@ var ADocEditor = function (customConfig) { ...@@ -535,9 +582,9 @@ var ADocEditor = function (customConfig) {
if (caretData.index == 0) { if (caretData.index == 0) {
let activeDataIndex = dataList.findIndex(item => item.id == caretData.activeData.id) let activeDataIndex = dataList.findIndex(item => item.id == caretData.activeData.id)
if (caretData.activeData.tabCount) { if (caretData.activeData.tabCount) {
if (caretData.activeData.type==1){ if (caretData.activeData.type == 1) {
caretData.activeData.type = 0 caretData.activeData.type = 0
}else{ } else {
caretData.activeData.tabCount-- caretData.activeData.tabCount--
} }
} }
...@@ -568,10 +615,10 @@ var ADocEditor = function (customConfig) { ...@@ -568,10 +615,10 @@ var ADocEditor = function (customConfig) {
} }
dataObj.plainContent = caretData.activeData.plainContent.slice(caretData.index) dataObj.plainContent = caretData.activeData.plainContent.slice(caretData.index)
caretData.activeData.plainContent = caretData.activeData.plainContent.slice(0,caretData.index) caretData.activeData.plainContent = caretData.activeData.plainContent.slice(0, caretData.index)
dataObj.formatedText = caretData.activeData.formatedText.splice(caretData.index) dataObj.formatedText = caretData.activeData.formatedText.splice(caretData.index)
let activeDataIndex = dataList.findIndex( item => item.id==caretData.activeData.id ) let activeDataIndex = dataList.findIndex(item => item.id == caretData.activeData.id)
dataList.splice( activeDataIndex+1, 0, dataObj ) dataList.splice(activeDataIndex + 1, 0, dataObj)
if (e.ctrlKey || e.metaKey) dataObj.newPage = true if (e.ctrlKey || e.metaKey) dataObj.newPage = true
caretData.index = 0 caretData.index = 0
...@@ -665,7 +712,8 @@ var ADocEditor = function (customConfig) { ...@@ -665,7 +712,8 @@ var ADocEditor = function (customConfig) {
const pxY = (e.clientY - rect.top) * config.pageSetup.pxHeight / rect.height; const pxY = (e.clientY - rect.top) * config.pageSetup.pxHeight / rect.height;
placeCaret({ x: pxX, y: pxY }) placeCaret({ x: pxX, y: pxY })
let style = caretData.activeData.formatedText[caretData.index] let style = caretData.activeData.formatedText[caretData.index]
if (style) caretData.style = { ...caretData.style, if (style) caretData.style = {
...caretData.style,
fontSize: style.fontSize, fontSize: style.fontSize,
} }
shadow.querySelector('[adc="font-size-input"]').value = caretData.style.fontSize shadow.querySelector('[adc="font-size-input"]').value = caretData.style.fontSize
...@@ -682,16 +730,16 @@ var ADocEditor = function (customConfig) { ...@@ -682,16 +730,16 @@ var ADocEditor = function (customConfig) {
} }
} }
function changeListStyle(){ function changeListStyle() {
let activeDataIndex = dataList.findIndex( item => item.id == caretData.activeData.id ) let activeDataIndex = dataList.findIndex(item => item.id == caretData.activeData.id)
let d = activeDataIndex-1 let d = activeDataIndex - 1
while( dataList?.[d]?.type==1){ while (dataList?.[d]?.type == 1) {
dataList[d].listStyle = caretData.activeData.listStyle dataList[d].listStyle = caretData.activeData.listStyle
--d --d
} }
d = activeDataIndex+1 d = activeDataIndex + 1
while(dataList?.[d]?.type==1){ while (dataList?.[d]?.type == 1) {
dataList[d].listStyle = caretData.activeData.listStyle dataList[d].listStyle = caretData.activeData.listStyle
++d ++d
} }
...@@ -793,11 +841,11 @@ var ADocEditor = function (customConfig) { ...@@ -793,11 +841,11 @@ var ADocEditor = function (customConfig) {
let charData = caretData.activeData.formatedText[caretData.index - 1] let charData = caretData.activeData.formatedText[caretData.index - 1]
if (lineObj) { if (lineObj) {
x = (charData?(charData.x+charData.width):x) x = (charData ? (charData.x + charData.width) : x)
if(charData){ if (charData) {
y = charData.y - (caretData.style.fontSize*pxMmRatio) y = charData.y - (caretData.style.fontSize * pxMmRatio)
}else{ } else {
y = lineObj.y - (lineObj.maxFontSize*pxMmRatio) y = lineObj.y - (lineObj.maxFontSize * pxMmRatio)
} }
} }
...@@ -836,28 +884,83 @@ var ADocEditor = function (customConfig) { ...@@ -836,28 +884,83 @@ var ADocEditor = function (customConfig) {
} }
} }
var returnObj = { function newDocument(e) {
getDocumentData(){ caretData.blink = true
let activeDataIndex = dataList.findIndex( item => item.id == caretData.activeData.id ) dataList = [
{
id: ++counter,
type: 0,
formatedText: [],
plainContent: "",
tabCount: 0,
style: { ...config.style },
listItemNumber: 0,
},
]
caretData = {
activeData: dataList[0],
index: 0,
timeout: null,
timeoutDuration: 800,
blink: true,
pageIndex: 0,
caretSize: config.style.fontSize,
previousCaret: null,
style: { ...dataList[0].style, x: config.format.margin, y: config.format.margin + (3 * config.style.fontSize / 4) },
}
pageList = []
shadow.querySelector('[adc="page-list"').innerHTML = ""
reRenderCanvas()
}
function downloadJSON() {
let data = compressDocJSON()
let jsonString = JSON.stringify(data)
const blob = new Blob([jsonString], { type: 'application/json' });
const a = shadow.querySelector('[adc="download-link"');
a.download = 'saved-data.json';
a.href = URL.createObjectURL(blob);
document.body.appendChild(a);
a.click();
}
function compressDocJSON() {
let activeDataIndex = dataList.findIndex(item => item.id == caretData.activeData.id)
let returnObj = { let returnObj = {
config: JSON.parse( JSON.stringify(config) ), config: JSON.parse(JSON.stringify(config)),
content: JSON.parse( JSON.stringify(dataList) ), content: JSON.parse(JSON.stringify(dataList)),
caretData: JSON.parse( JSON.stringify(caretData) ) caretData: JSON.parse(JSON.stringify(caretData))
} }
delete returnObj.caretData.activeData delete returnObj.caretData.activeData
delete returnObj.caretData.previousCaret
returnObj.caretData.activeDataIndex = activeDataIndex returnObj.caretData.activeDataIndex = activeDataIndex
returnObj.compressed = true
return returnObj return returnObj
}
},
setDocumentData(data){ function setDocumentData(data) {
// might need to change some configuration // might need to change some configuration
dataList = JSON.parse( JSON.stringify( data.content ) )
config = JSON.parse( JSON.stringify( data.config ) ) dataList = JSON.parse(JSON.stringify(data.content))
caretData = JSON.parse( JSON.stringify( data.caretData ) ) config = JSON.parse(JSON.stringify(data.config))
caretData = JSON.parse(JSON.stringify(data.caretData))
caretData.activeData = dataList[data.caretData.activeDataIndex] caretData.activeData = dataList[data.caretData.activeDataIndex]
caretData.blink = false caretData.blink = false
reRenderCanvas() reRenderCanvas()
}
var returnObj = {
getDocumentData() {
return compressDocJSON()
}, },
setDocumentData,
addFonts, addFonts,
getConfiguration() { return JSON.parse(JSON.stringify(config)) }, getConfiguration() { return JSON.parse(JSON.stringify(config)) },
getPages() { getPages() {
...@@ -866,9 +969,7 @@ var ADocEditor = function (customConfig) { ...@@ -866,9 +969,7 @@ var ADocEditor = function (customConfig) {
return pagesToReturn return pagesToReturn
}, },
log() { log() {
console.log('dataList', dataList) console.log(compressDocJSON())
console.log('lines', lines)
console.log('caretData', caretData)
}, },
focusOnPage, focusOnPage,
} }
......
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