Модуль:ТранспортнаяСхема: различия между версиями

Нет описания правки
мНет описания правки
Метка: ручная отмена
 
(не показано 5 промежуточных версий этого же участника)
Строка 16: Строка 16:
end
end


-- Загрузка данных из data.json с расширенным поиском
-- Безопасная очистка текста JSON
local function cleanJsonText(rawText)
    rawText = mw.ustring.gsub(rawText, '<noinclude>.-</noinclude>', '')
    rawText = string.gsub(rawText, '<!%-%-.-%-%->', '')
    rawText = string.gsub(rawText, '\194\160', ' ')
    rawText = string.gsub(rawText, '\194\173', '')
    return mw.text.trim(rawText)
end
 
-- Загрузка данных из data.json
local function loadData(args)
local function loadData(args)
     local dataParam = args['данные'] or args[1] or 'Муроморского'
     local dataParam = args['данные'] or args[1] or 'Муроморского'
    dataParam = mw.text.trim(dataParam)
   
    local cleanName = mw.ustring.gsub(dataParam, '^[Шш][Аа][Бб][Лл][Оо][Нн]:', '')
    cleanName = mw.ustring.gsub(cleanName, '^[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee]:', '')
    cleanName = mw.ustring.gsub(cleanName, '/data%.json$', '')
    cleanName = mw.ustring.gsub(cleanName, '%.json$', '')
    cleanName = mw.ustring.gsub(cleanName, '^[Ии]нтерактивная схема%s*', '')
    cleanName = mw.text.trim(cleanName)
      
      
     local candidates = {}
     local candidates = {}
Строка 30: Строка 47:
     end
     end
      
      
    -- Проверяем прямое имя, основное пространство и пространство Шаблон:
     addCandidate(dataParam)
     addCandidate(dataParam)
     addCandidate(dataParam .. '/data.json')
     addCandidate('Шаблон:Интерактивная схема ' .. cleanName .. '/data.json')
     addCandidate('Шаблон:' .. dataParam)
     addCandidate('Шаблон:Интерактивная схема ' .. cleanName .. ' метрополитена/data.json')
     addCandidate('Шаблон:' .. dataParam .. '/data.json')
     addCandidate('Шаблон:' .. cleanName .. '/data.json')
     addCandidate('Шаблон:Интерактивная схема ' .. dataParam .. '/data.json')
     addCandidate('Шаблон:' .. cleanName)
     addCandidate('Шаблон:Интерактивная схема ' .. dataParam .. ' метрополитена/data.json')
     addCandidate('Интерактивная схема ' .. cleanName .. '/data.json')
     addCandidate('Интерактивная схема ' .. dataParam .. '/data.json')
     addCandidate('Интерактивная схема ' .. cleanName .. ' метрополитена/data.json')
     addCandidate('Интерактивная схема ' .. dataParam .. ' метрополитена/data.json')
     addCandidate(cleanName .. '/data.json')
      
     addCandidate(cleanName)
 
     local jsonErrors = {}
     local jsonErrors = {}
      
      
     for _, pageTitle in ipairs(candidates) do
     for _, pageTitle in ipairs(candidates) do
        -- 1. Загрузка через mw.loadJsonData
         local ok, data = pcall(mw.loadJsonData, pageTitle)
         local ok, data = pcall(mw.loadJsonData, pageTitle)
         if ok and data and type(data) == 'table' then
         if ok and type(data) == 'table' then
             return data
             return data
        elseif not ok and data and not mw.ustring.find(tostring(data), 'does not exist') and not mw.ustring.find(tostring(data), 'не существует') then
            table.insert(jsonErrors, pageTitle .. ': ' .. tostring(data))
         end
         end
          
          
        -- 2. Загрузка текста через mw.title (если страница создана как вики-текст)
         local titleObj = mw.title.new(pageTitle)
         local titleObj = mw.title.new(pageTitle)
         if titleObj and titleObj.exists then
         if titleObj then
             local rawText = titleObj:getContent()
             local rawText = titleObj:getContent()
             if rawText and rawText ~= '' then
             if rawText and rawText ~= '' then
                 rawText = mw.ustring.gsub(rawText, '<noinclude>.-</noinclude>', '')
                 rawText = cleanJsonText(rawText)
                rawText = mw.ustring.gsub(rawText, '<!--.-?-->', '')
                rawText = mw.text.trim(rawText)
               
                 local decOk, decoded = pcall(mw.text.jsonDecode, rawText)
                 local decOk, decoded = pcall(mw.text.jsonDecode, rawText)
                 if decOk and decoded and type(decoded) == 'table' then
                 if decOk and type(decoded) == 'table' then
                     return decoded
                     return decoded
                 else
                 else
                     table.insert(jsonErrors, pageTitle .. ' (ошибка синтаксиса JSON: ' .. tostring(decoded) .. ')')
                     table.insert(jsonErrors, '[[' .. pageTitle .. ']] (' .. tostring(decoded) .. ')')
                 end
                 end
             end
             end
Строка 71: Строка 81:
      
      
     if #jsonErrors > 0 then
     if #jsonErrors > 0 then
         error('Ошибка в синтаксисе JSON на найденной странице:\n* ' .. table.concat(jsonErrors, '\n* '))
         error('Ошибка в синтаксисе JSON:\n* ' .. table.concat(jsonErrors, '\n* '))
     end
     end
      
      
     error('Страница данных JSON не найдена. Убедитесь, что страница создана и сохранена под одним из названий:\n* [[' .. table.concat(candidates, ']]\n* [[') .. ']]')
     error('Страница данных не найдена ни по одному из путей:\n* [[' .. table.concat(candidates, ']]\n* [[') .. ']]')
end
end


Строка 84: Строка 94:
end
end


-- Генерация Canvas схемы
-- Генерация интерактивной Canvas-схемы с панорамированием и управлением
function p.canvas(frame)
function p.canvas(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)
Строка 92: Строка 102:
     local origH = tonumber(args['высота_оригинала'] or 620) or 620
     local origH = tonumber(args['высота_оригинала'] or 620) or 620
      
      
    -- Кадрирование
     local cropX, cropY, cropW, cropH = nil, nil, nil, nil
     local cropX, cropY, cropW, cropH = nil, nil, nil, nil
     if args['кадрирование'] or args['crop'] then
     if args['кадрирование'] or args['crop'] then
Строка 114: Строка 125:
     local scale = tonumber(args['масштаб'] or args['scale'])
     local scale = tonumber(args['масштаб'] or args['scale'])
      
      
     if boxW and not scale then
    local isScrollMode = (args['прокрутка'] == 'да' or args['скролл'] == 'да' or args['scroll'] == 'yes')
   
     if not isScrollMode and boxW and not scale then
         scale = boxW / targetW
         scale = boxW / targetW
     elseif not scale then
     elseif not scale then
Строка 123: Строка 136:
     local finalBoxH = tonumber(args['высота_блока'] or args['высота_карточки'] or args['высота']) or (targetH * scale)
     local finalBoxH = tonumber(args['высота_блока'] or args['высота_карточки'] or args['высота']) or (targetH * scale)
      
      
    -- Внешний контейнер-вьюпорт с поддержкой перемещения клавиатурой и скролла
     local wrapper = mw.html.create('div')
     local wrapper = mw.html.create('div')
         :addClass('ts-transport-map-viewport')
         :addClass('ts-transport-map-viewport')
        :attr('tabindex', '0')
        :attr('title', 'Кликните для управления стрелками клавиатуры (← ↑ → ↓) или скролла мышью')
         :css({
         :css({
             ['position'] = 'relative',
             ['position'] = 'relative',
Строка 130: Строка 146:
             ['height'] = string.format('%.1fpx', finalBoxH),
             ['height'] = string.format('%.1fpx', finalBoxH),
             ['max-width'] = '100%',
             ['max-width'] = '100%',
             ['overflow'] = 'hidden',
             ['overflow'] = isScrollMode and 'auto' or 'hidden',
             ['border-radius'] = '8px',
            ['scroll-behavior'] = 'smooth',
            ['overscroll-behavior'] = 'contain',
            ['-webkit-overflow-scrolling'] = 'touch',
             ['border-radius'] = '10px',
             ['border'] = args['без_рамки'] and 'none' or '1px solid #d0d7de',
             ['border'] = args['без_рамки'] and 'none' or '1px solid #d0d7de',
             ['margin'] = (args['прижатие'] == 'лево' and '0 auto 0 0') or (args['прижатие'] == 'право' and '0 0 0 auto') or '0 auto',
             ['margin'] = (args['прижатие'] == 'лево' and '0 auto 0 0') or (args['прижатие'] == 'право' and '0 0 0 auto') or '0 auto',
Строка 137: Строка 156:
             ['background-image'] = 'radial-gradient(#e1e4e8 1px, transparent 1px)',
             ['background-image'] = 'radial-gradient(#e1e4e8 1px, transparent 1px)',
             ['background-size'] = '20px 20px',
             ['background-size'] = '20px 20px',
             ['box-shadow'] = args['без_тени'] and 'none' or '0 2px 8px rgba(0,0,0,0.05)',
             ['box-shadow'] = args['без_тени'] and 'none' or '0 2px 8px rgba(0,0,0,0.06)',
            ['outline'] = 'none',
             ['user-select'] = 'none'
             ['user-select'] = 'none'
         })
         })
Строка 144: Строка 164:
     local shiftY = cropY or 0
     local shiftY = cropY or 0
      
      
    -- Внутренний холст схемы
     local canvas = wrapper:tag('div')
     local canvas = wrapper:tag('div')
         :addClass('ts-html-transport-canvas')
         :addClass('ts-html-transport-canvas')
         :css({
         :css({
             ['position'] = 'absolute',
             ['position'] = isScrollMode and 'relative' or 'absolute',
             ['left'] = string.format('%.1fpx', -shiftX * scale),
             ['left'] = not isScrollMode and string.format('%.1fpx', -shiftX * scale) or '0',
             ['top'] = string.format('%.1fpx', -shiftY * scale),
             ['top'] = not isScrollMode and string.format('%.1fpx', -shiftY * scale) or '0',
             ['width'] = origW .. 'px',
             ['width'] = origW .. 'px',
             ['height'] = origH .. 'px',
             ['height'] = origH .. 'px',
             ['transform'] = string.format('scale(%.4f)', scale),
             ['transform'] = (scale ~= 1.0 and not isScrollMode) and string.format('scale(%.4f)', scale) or 'none',
             ['transform-origin'] = '0 0',
             ['transform-origin'] = '0 0',
             ['font-family'] = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
             ['font-family'] = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
         })
         })


    -- Шапка/логотип
     if not (args['шапка'] == 'нет' or cropX ~= nil) then
     if not (args['шапка'] == 'нет' or cropX ~= nil) then
         local header = canvas:tag('div')
         local header = canvas:tag('div')
Строка 190: Строка 212:
             :tag('div'):css({['font-weight'] = 'bold', ['font-size'] = '13px', ['color'] = '#1f2328'}):wikitext(data.title or 'Скоростной транспорт'):done()
             :tag('div'):css({['font-weight'] = 'bold', ['font-size'] = '13px', ['color'] = '#1f2328'}):wikitext(data.title or 'Скоростной транспорт'):done()
             :tag('div'):css({['font-size'] = '10px', ['color'] = '#57606a', ['text-transform'] = 'uppercase', ['letter-spacing'] = '0.5px'}):wikitext('Интерактивная схема линий'):done()
             :tag('div'):css({['font-size'] = '10px', ['color'] = '#57606a', ['text-transform'] = 'uppercase', ['letter-spacing'] = '0.5px'}):wikitext('Интерактивная схема линий'):done()
    end
    -- Индикатор подсказки управления
    if isScrollMode then
        wrapper:tag('div')
            :css({
                ['position'] = 'sticky',
                ['top'] = '10px',
                ['left'] = 'calc(100% - 130px)',
                ['width'] = '115px',
                ['z-index'] = '10',
                ['background'] = 'rgba(255,255,255,0.88)',
                ['border'] = '1px solid #d0d7de',
                ['border-radius'] = '16px',
                ['padding'] = '3px 8px',
                ['font-size'] = '10px',
                ['color'] = '#57606a',
                ['display'] = 'flex',
                ['align-items'] = 'center',
                ['gap'] = '4px',
                ['pointer-events'] = 'none',
                ['box-shadow'] = '0 1px 4px rgba(0,0,0,0.05)',
                ['backdrop-filter'] = 'blur(2px)'
            })
            :wikitext('⌨ ⇄ ⇅ Скролл')
     end
     end


     if data.lines then
     if data.lines then
         -- Слой путей
         -- 1. Слой путей
         for _, line in ipairs(data.lines) do
         for _, line in ipairs(data.lines) do
             local color = line.color or '#888888'
             local color = line.color or '#888888'
Строка 229: Строка 276:
         end
         end


         -- Слой станций и подписей
         -- 2. Слой станций и подписей
         for _, line in ipairs(data.lines) do
         for _, line in ipairs(data.lines) do
             local color = line.color or '#888888'
             local color = line.color or '#888888'