将多级列表扩展到级别 2 和 3

Expanding Multi level list to level 2 and 3

提问人:Emre Karayalcin 提问时间:12/10/2019 最后编辑:Emre Karayalcin 更新时间:12/12/2019 访问量:175

问:

我目前有一个大纲编辑器,当我输入选项卡时,它会向前移动 1 级,并带有一个项目符号,当我按回车键时,它会将项目符号放置在 0 级。我的目标是尝试将 tab 键设置为可以转到级别 2 和 3 的位置,但是,我避免使用 jQuery。我可能在下面的代码中采取了错误的方法,但任何帮助将不胜感激。到目前为止,我已经使用了附加子项的方法,但不确定如何以附加子项的方式处理它。

const TAB_KEY = 9;
const ENTER_KEY = 13;
const SHIFT_KEY = 16

const editor = document.querySelector('.editor');

    editor.appendChild(document.createElement('li'));

editor.addEventListener('keydown', (e) => {
  let code = e.keyCode || e.which;
  if (code == TAB_KEY) {
    e.preventDefault();
    let parent = e.target;
    let ul = document.createElement('ul');
    let li = document.createElement('li');
    ul.appendChild(li);
    parent.appendChild(ul);
    moveCursorToEnd(li);
  } else if (code == ENTER_KEY) {
    e.preventDefault();
    let parent = e.target;
    let li = document.createElement('li');
    parent.appendChild(li);
    moveCursorToEnd(li);
  } else if (code == TAB_KEY * TAB_KEY){
    e.preventDefault();
    let parent = e.target;
    let ol = document.createElement('ol');
    let li = document.createElement('li');
    ol.appendChild(li);
    parent.appendChild(ol);
    moveCursorToEnd(li);
  }
});

function moveCursorToEnd(el) {
  el.focus();
  document.execCommand('selectAll', false, null);
  document.getSelection().collapseToEnd();
}

function saveTextFile(){
  let textToSave = document.getElementById("text").innerHTML;
  let textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
  let textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
  let fileName = document.getElementById("saveAs").value;

  let downloadLink = document.createElement("a");
  downloadLink.download = fileName;
  downloadLink.innerHTML = "Download File";
  downloadLink.href = textToSaveAsURL;
  downloadLink.onclick = destroyClickedElement;
  downloadLink.style.display = "none";
  document.body.appendChild(downloadLink);

  downloadLink.click();
}
function destroyClickedElement(event){
  document.body.removeChild(event.target);
}

function loadFile(){
  let fileLoad = document.getElementById("load").files[0];

  let fileReader = new FileReader();
  fileReader.onload = function(fileLoadedEvent){

    let textFromFileLoaded = fileLoadedEvent.target.result;
    document.getElementById("text").innerHTML = textFromFileLoaded;
  };
  fileReader.readAsText(fileLoad, "UTF-8");
}

/*editor.addEventListener('click', (x) => {
  x = document.getElementById("b");
  if(x.style.fontWeight == "bolder"){
    x.style.fontWeight = "normal";
  } else {
    x.style.fontWeight = "bolder";
  }
});*/

function bold(){
if(document.execCommand("bold")){
  document.execCommand("normal");
}else{
  document.execCommand("bold");
}
}
/*function underline(){
let x = document.getElementById("text");
if(x.style.textDecoration == "underline"){
  x.style.textDecoration = "none";
}else{
  x.style.textDecoration = "underline";
}
}*/

function underline(){
if(document.execCommand("underline")){
  document.execCommand("none");
}else{
  document.execCommand("underline");
}
}

/*function italic(){
let x = document.getElementById("text");
if(x.style.fontStyle == "italic"){
  x.style.fontStyle = "normal";
}else{
  x.style.fontStyle = "italic";
}
}*/


/*Turns the font of the text to Italic*/
function italic(){
if(document.execCommand("italic")){
  document.execCommand("normal");
}else{
  document.execCommand("italic");
}
}

function highlighSelectedText(){
  let sel = window.getSelection().getRangeAt(0);
  let selText = sel.extractContents();
  let span = document.createElement("span");
  span.style.backgroundColor = "yellow";
  span.appendChild(selText);
  sel.insertNode(span);
}


/*function printPage(){
  let printButton = document.getElementById("ul");
  printButton.style.visibility = 'hidden';
  window.print();
  printButton.style.visibility = 'visible';
}*/
body{
  margin-top:1em;
  margin-bottom: 10em;
  margin-right: 1em;
  margin-left: 1em;
  border: solid;
  border-color: #0033cc;
  background-color: #f6f6f6;
}

 div button{
  padding: 1em 2em;
  color: white;
  background-color: #0000cc;
}
 div input{
  padding: 1em 2em;
  color: white;
  background-color: #0000cc;
}

div{
  list-style-type:square;
  list-style-position: inside;
  margin-left: 0.25em;
  margin-bottom: 5em;
}

section {
  padding: 1em 2em;
  color: white;
  background-color: #0000cc;
}
.editor {
  font-weight: normal;
}

div contenteditable{
  margin-bottom: 10em;
}
<!DOCTYPE html>
<meta charset="utf-8">
<body>
  <head>
    <title>Outliner</title>
    <link href="style.css" rel="stylesheet" title="Style">
    <div>
      <button id="b" onclick="bold()"> B </button>
      <button onclick="underline()"> U </button>
      <button onclick="italic()"> I </button>
      <input type="button" onclick="highlighSelectedText()"  value="Highlight"/>
      <input id="color" type="color">
      <button onclick="document.getElementById('text').style.color = document.getElementById('color').value">Set Colour</button>
      <div id="text" class="editor" contenteditable="true" draggable="true"></div>
    </div>
<section>
    <input id="saveAs"></input>
    <button onclick="saveTextFile()">Download</button>
    <input type="file" id="load"/>
    <button onclick="loadFile()">Load</button>
  </section>
  </head>
  <script type= "text/javascript" src='setting.js'></script>
</body>

JavaScript HTML CSS 列表 嵌套列表

评论

0赞 Ivan86 12/11/2019
请包含更多代码,以便我们可以运行和测试它。现在缺少一些函数(如bold(),underline(),...
0赞 Emre Karayalcin 12/11/2019
我包含了代码的其余部分。现在应该一切正常

答: 暂无答案