quick and dirty title support implementation

This commit is contained in:
Jean-Tiare Le Bigot 2012-03-02 14:40:46 +01:00
parent 1d8dd16a21
commit 9a5ae726bc
3 changed files with 79 additions and 25 deletions

View file

@ -2975,6 +2975,7 @@ function Ace2Inner(){
return str.replace(/[\n\r ]/g, ' ').replace(/\xa0/g, ' ').replace(/\t/g, ' '); return str.replace(/[\n\r ]/g, ' ').replace(/\xa0/g, ' ').replace(/\t/g, ' ');
} }
//FIXME: duplicated with contentcollector.js
var _blockElems = { var _blockElems = {
//blocks and paragraphs //blocks and paragraphs
"div": 1, "div": 1,
@ -5186,13 +5187,8 @@ function Ace2Inner(){
// -> title // -> title
//"defaultLevel" => directly create at a given nesting level. This is especially //"defaultLevel" => directly create at a given nesting level. This is especially
// usefull for titles wich can be created at arbitrary level (h1->hx) // usefull for titles wich can be created at arbitrary level (h1->hx)
function doInsertList(type, defaultLevel) function doInsertList(type, requestLevel)
{ {
if(!defaultLevel)
{
defaultLevel = 1;
}
if (!(rep.selStart && rep.selEnd)) if (!(rep.selStart && rep.selEnd))
{ {
return; return;
@ -5202,30 +5198,56 @@ function Ace2Inner(){
firstLine = rep.selStart[0]; firstLine = rep.selStart[0];
lastLine = Math.max(firstLine, rep.selEnd[0] - ((rep.selEnd[1] === 0) ? 1 : 0)); lastLine = Math.max(firstLine, rep.selEnd[0] - ((rep.selEnd[1] === 0) ? 1 : 0));
var allLinesAreList = true; var allLinesAreList = true && !requestLevel;
for (var n = firstLine; n <= lastLine; n++) //if a specific level is requested, we do not care about the current status
if(!requestLevel)
{ {
var listType = getLineListType(n); for (var n = firstLine; n <= lastLine; n++)
if (!listType || listType.slice(0, type.length) != type)
{ {
allLinesAreList = false; var listType = getLineListType(n);
break; if (!listType || listType.slice(0, type.length) != type)
{
allLinesAreList = false;
break;
}
} }
} }
//compute the modification to apply for each single line in the selection
//we try to do only what is requested and as such to preserve the indentation
//level in case of list type transformation even if the list is composed of
//several indentation levels
//In case a specific level is requested. Force it
//if all lines are already of this list type, we remove the list
//otherwise, we apply the new one
var mods = []; var mods = [];
for (var n = firstLine; n <= lastLine; n++) for (var n = firstLine; n <= lastLine; n++)
{ {
var t = ''; var t = '';
var level = 0; var level = 0;
var listType = /([a-z]+)([12345678])/.exec(getLineListType(n)); var listType = /([a-z]+)([12345678])/.exec(getLineListType(n));
if (listType) if (listType)//already a list => read attributes
{ {
t = listType[1]; t = listType[1];
level = Number(listType[2]); level = Number(listType[2]);
} }
var t = getLineListType(n); var t = getLineListType(n);
mods.push([n, allLinesAreList ? 'indent' + level : (t ? type + level : type + defaultLevel)]); //apply the list type. If a specific level is requested, force it
if(requestLevel)
{
if(type+level == type+requestLevel)
{
type = 'indent';
level = 0;
}
else
{
level = requestLevel;
}
}
alert(allLinesAreList ? 'indent' + level : (t ? type + level : type + '1'));
mods.push([n, allLinesAreList ? 'indent' + level : (t ? type + level : type + '1')]);
} }
setLineListTypes(mods); setLineListTypes(mods);
} }

View file

@ -74,11 +74,21 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
} }
}; };
//FIXME: duplicated with ace2_inner.js and lower in this file !
var _blockElems = { var _blockElems = {
//blocks and paragraphs
"div": 1, "div": 1,
"p": 1, "p": 1,
"pre": 1, "pre": 1,
"li": 1 //lists styles
"li": 1,
"ol": 1,
"ul": 1,
//titling styles
"h1": 1,
"h2": 1,
"h3": 1,
"h4": 1,
}; };
function isBlockElement(n) function isBlockElement(n)
@ -163,10 +173,22 @@ function makeContentCollector(collectStyles, browser, apool, domInterface, class
var selection, startPoint, endPoint; var selection, startPoint, endPoint;
var selStart = [-1, -1], var selStart = [-1, -1],
selEnd = [-1, -1]; selEnd = [-1, -1];
var blockElems = {
//FIXME: duplicated with ace2_inner.js and even above !
var _blockElems = {
//blocks and paragraphs
"div": 1, "div": 1,
"p": 1, "p": 1,
"pre": 1 "pre": 1,
//lists styles
"li": 1,
"ol": 1,
"ul": 1,
//titling styles
"h1": 1,
"h2": 1,
"h3": 1,
"h4": 1,
}; };
function _isEmpty(node, state) function _isEmpty(node, state)

View file

@ -104,16 +104,26 @@ domline.createDomLine = function(nonEmpty, doesWrap, optBrowser, optDocument)
start = start?'start="'+Security.escapeHTMLAttribute(start[1])+'"':''; start = start?'start="'+Security.escapeHTMLAttribute(start[1])+'"':'';
if (listType) if (listType)
{ {
if(listType.indexOf("number") < 0) var details = /([a-z]+)([12345678])/.exec(listType);
{ //handle numbered lists
preHtml = '<ul class="list-' + Security.escapeHTMLAttribute(listType) + '"><li>'; if(details[1] == "number")
postHtml = '</li></ul>';
}
else
{ {
preHtml = '<ol '+start+' class="list-' + Security.escapeHTMLAttribute(listType) + '"><li>'; preHtml = '<ol '+start+' class="list-' + Security.escapeHTMLAttribute(listType) + '"><li>';
postHtml = '</li></ol>'; postHtml = '</li></ol>';
} }
//handle titles
else if(details[1] == "title")
{
markup = 'h'+Number(details[2]);
preHtml = '<'+markup+'>';
postHtml = '</'+markup+'>';
}
//handle bullet lists and indentations
else
{
preHtml = '<ul class="list-' + Security.escapeHTMLAttribute(listType) + '"><li>';
postHtml = '</li></ul>';
}
} }
result.lineMarker += txt.length; result.lineMarker += txt.length;
return; // don't append any text return; // don't append any text